Merge branch 'mlx5-next' of git://git.kernel.org/pub/scm/linux/kernel/git/mellanox...
[sfrench/cifs-2.6.git] / drivers / net / phy / mdio-mux-gpio.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2011, 2012 Cavium, Inc.
7  */
8
9 #include <linux/platform_device.h>
10 #include <linux/device.h>
11 #include <linux/of_mdio.h>
12 #include <linux/module.h>
13 #include <linux/phy.h>
14 #include <linux/mdio-mux.h>
15 #include <linux/gpio/consumer.h>
16
17 #define DRV_VERSION "1.1"
18 #define DRV_DESCRIPTION "GPIO controlled MDIO bus multiplexer driver"
19
20 struct mdio_mux_gpio_state {
21         struct gpio_descs *gpios;
22         void *mux_handle;
23         int values[];
24 };
25
26 static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,
27                                    void *data)
28 {
29         struct mdio_mux_gpio_state *s = data;
30         unsigned int n;
31
32         if (current_child == desired_child)
33                 return 0;
34
35         for (n = 0; n < s->gpios->ndescs; n++)
36                 s->values[n] = (desired_child >> n) & 1;
37
38         gpiod_set_array_value_cansleep(s->gpios->ndescs, s->gpios->desc,
39                                        s->values);
40
41         return 0;
42 }
43
44 static int mdio_mux_gpio_probe(struct platform_device *pdev)
45 {
46         struct mdio_mux_gpio_state *s;
47         struct gpio_descs *gpios;
48         int r;
49
50         gpios = gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
51         if (IS_ERR(gpios))
52                 return PTR_ERR(gpios);
53
54         s = devm_kzalloc(&pdev->dev, struct_size(s, values, gpios->ndescs),
55                          GFP_KERNEL);
56         if (!s) {
57                 gpiod_put_array(gpios);
58                 return -ENOMEM;
59         }
60
61         s->gpios = gpios;
62
63         r = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
64                           mdio_mux_gpio_switch_fn, &s->mux_handle, s, NULL);
65
66         if (r != 0) {
67                 gpiod_put_array(s->gpios);
68                 return r;
69         }
70
71         pdev->dev.platform_data = s;
72         return 0;
73 }
74
75 static int mdio_mux_gpio_remove(struct platform_device *pdev)
76 {
77         struct mdio_mux_gpio_state *s = dev_get_platdata(&pdev->dev);
78         mdio_mux_uninit(s->mux_handle);
79         gpiod_put_array(s->gpios);
80         return 0;
81 }
82
83 static const struct of_device_id mdio_mux_gpio_match[] = {
84         {
85                 .compatible = "mdio-mux-gpio",
86         },
87         {
88                 /* Legacy compatible property. */
89                 .compatible = "cavium,mdio-mux-sn74cbtlv3253",
90         },
91         {},
92 };
93 MODULE_DEVICE_TABLE(of, mdio_mux_gpio_match);
94
95 static struct platform_driver mdio_mux_gpio_driver = {
96         .driver = {
97                 .name           = "mdio-mux-gpio",
98                 .of_match_table = mdio_mux_gpio_match,
99         },
100         .probe          = mdio_mux_gpio_probe,
101         .remove         = mdio_mux_gpio_remove,
102 };
103
104 module_platform_driver(mdio_mux_gpio_driver);
105
106 MODULE_DESCRIPTION(DRV_DESCRIPTION);
107 MODULE_VERSION(DRV_VERSION);
108 MODULE_AUTHOR("David Daney");
109 MODULE_LICENSE("GPL");