Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2011, 2012 Cavium, Inc.
0004  */
0005 
0006 #include <linux/device.h>
0007 #include <linux/gpio/consumer.h>
0008 #include <linux/mdio-mux.h>
0009 #include <linux/module.h>
0010 #include <linux/of_mdio.h>
0011 #include <linux/phy.h>
0012 #include <linux/platform_device.h>
0013 
0014 #define DRV_VERSION "1.1"
0015 #define DRV_DESCRIPTION "GPIO controlled MDIO bus multiplexer driver"
0016 
0017 struct mdio_mux_gpio_state {
0018     struct gpio_descs *gpios;
0019     void *mux_handle;
0020 };
0021 
0022 static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,
0023                    void *data)
0024 {
0025     struct mdio_mux_gpio_state *s = data;
0026     DECLARE_BITMAP(values, BITS_PER_TYPE(desired_child));
0027 
0028     if (current_child == desired_child)
0029         return 0;
0030 
0031     values[0] = desired_child;
0032 
0033     gpiod_set_array_value_cansleep(s->gpios->ndescs, s->gpios->desc,
0034                        s->gpios->info, values);
0035 
0036     return 0;
0037 }
0038 
0039 static int mdio_mux_gpio_probe(struct platform_device *pdev)
0040 {
0041     struct mdio_mux_gpio_state *s;
0042     struct gpio_descs *gpios;
0043     int r;
0044 
0045     gpios = devm_gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
0046     if (IS_ERR(gpios))
0047         return PTR_ERR(gpios);
0048 
0049     s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
0050     if (!s)
0051         return -ENOMEM;
0052 
0053     s->gpios = gpios;
0054 
0055     r = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
0056               mdio_mux_gpio_switch_fn, &s->mux_handle, s, NULL);
0057 
0058     if (r != 0)
0059         return r;
0060 
0061     pdev->dev.platform_data = s;
0062     return 0;
0063 }
0064 
0065 static int mdio_mux_gpio_remove(struct platform_device *pdev)
0066 {
0067     struct mdio_mux_gpio_state *s = dev_get_platdata(&pdev->dev);
0068     mdio_mux_uninit(s->mux_handle);
0069     return 0;
0070 }
0071 
0072 static const struct of_device_id mdio_mux_gpio_match[] = {
0073     {
0074         .compatible = "mdio-mux-gpio",
0075     },
0076     {
0077         /* Legacy compatible property. */
0078         .compatible = "cavium,mdio-mux-sn74cbtlv3253",
0079     },
0080     {},
0081 };
0082 MODULE_DEVICE_TABLE(of, mdio_mux_gpio_match);
0083 
0084 static struct platform_driver mdio_mux_gpio_driver = {
0085     .driver = {
0086         .name       = "mdio-mux-gpio",
0087         .of_match_table = mdio_mux_gpio_match,
0088     },
0089     .probe      = mdio_mux_gpio_probe,
0090     .remove     = mdio_mux_gpio_remove,
0091 };
0092 
0093 module_platform_driver(mdio_mux_gpio_driver);
0094 
0095 MODULE_DESCRIPTION(DRV_DESCRIPTION);
0096 MODULE_VERSION(DRV_VERSION);
0097 MODULE_AUTHOR("David Daney");
0098 MODULE_LICENSE("GPL v2");