691ab9dd623676cdf64f3e8f641eeb75083dfb9d
[linux-2.6-microblaze.git] / drivers / mfd / ti-lmu.c
1 /*
2  * TI LMU (Lighting Management Unit) Core Driver
3  *
4  * Copyright 2017 Texas Instruments
5  *
6  * Author: Milo Kim <milo.kim@ti.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/ti-lmu.h>
20 #include <linux/mfd/ti-lmu-register.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/slab.h>
25
26 struct ti_lmu_data {
27         const struct mfd_cell *cells;
28         int num_cells;
29         unsigned int max_register;
30 };
31
32 static int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id)
33 {
34         if (lmu->en_gpio)
35                 gpiod_set_value(lmu->en_gpio, 1);
36
37         /* Delay about 1ms after HW enable pin control */
38         usleep_range(1000, 1500);
39
40         /* LM3631 has additional power up sequence - enable LCD_EN bit. */
41         if (id == LM3631) {
42                 return regmap_update_bits(lmu->regmap, LM3631_REG_DEVCTRL,
43                                           LM3631_LCD_EN_MASK,
44                                           LM3631_LCD_EN_MASK);
45         }
46
47         return 0;
48 }
49
50 static void ti_lmu_disable_hw(void *data)
51 {
52         struct ti_lmu *lmu = data;
53         if (lmu->en_gpio)
54                 gpiod_set_value(lmu->en_gpio, 0);
55 }
56
57 #define LM363X_REGULATOR(_id)                   \
58 {                                               \
59         .name          = "lm363x-regulator",    \
60         .id            = _id,                   \
61         .of_compatible = "ti,lm363x-regulator", \
62 }                                               \
63
64 static const struct mfd_cell lm3631_devices[] = {
65         LM363X_REGULATOR(LM3631_BOOST),
66         LM363X_REGULATOR(LM3631_LDO_CONT),
67         LM363X_REGULATOR(LM3631_LDO_OREF),
68         LM363X_REGULATOR(LM3631_LDO_POS),
69         LM363X_REGULATOR(LM3631_LDO_NEG),
70         {
71                 .name          = "ti-lmu-backlight",
72                 .id            = LM3631,
73                 .of_compatible = "ti,lm3631-backlight",
74         },
75 };
76
77 static const struct mfd_cell lm3632_devices[] = {
78         LM363X_REGULATOR(LM3632_BOOST),
79         LM363X_REGULATOR(LM3632_LDO_POS),
80         LM363X_REGULATOR(LM3632_LDO_NEG),
81         {
82                 .name          = "ti-lmu-backlight",
83                 .id            = LM3632,
84                 .of_compatible = "ti,lm3632-backlight",
85         },
86 };
87
88 static const struct mfd_cell lm3633_devices[] = {
89         {
90                 .name          = "ti-lmu-backlight",
91                 .id            = LM3633,
92                 .of_compatible = "ti,lm3633-backlight",
93         },
94         {
95                 .name          = "lm3633-leds",
96                 .of_compatible = "ti,lm3633-leds",
97         },
98         /* Monitoring driver for open/short circuit detection */
99         {
100                 .name          = "ti-lmu-fault-monitor",
101                 .id            = LM3633,
102                 .of_compatible = "ti,lm3633-fault-monitor",
103         },
104 };
105
106 static const struct mfd_cell lm3695_devices[] = {
107         {
108                 .name          = "ti-lmu-backlight",
109                 .id            = LM3695,
110                 .of_compatible = "ti,lm3695-backlight",
111         },
112 };
113
114 static const struct mfd_cell lm36274_devices[] = {
115         LM363X_REGULATOR(LM36274_BOOST),
116         LM363X_REGULATOR(LM36274_LDO_POS),
117         LM363X_REGULATOR(LM36274_LDO_NEG),
118         {
119                 .name          = "lm36274-leds",
120                 .id            = LM36274,
121                 .of_compatible = "ti,lm36274-backlight",
122         },
123 };
124
125 #define TI_LMU_DATA(chip, max_reg)              \
126 static const struct ti_lmu_data chip##_data =   \
127 {                                               \
128         .cells = chip##_devices,                \
129         .num_cells = ARRAY_SIZE(chip##_devices),\
130         .max_register = max_reg,                \
131 }                                               \
132
133 TI_LMU_DATA(lm3631, LM3631_MAX_REG);
134 TI_LMU_DATA(lm3632, LM3632_MAX_REG);
135 TI_LMU_DATA(lm3633, LM3633_MAX_REG);
136 TI_LMU_DATA(lm3695, LM3695_MAX_REG);
137 TI_LMU_DATA(lm36274, LM36274_MAX_REG);
138
139 static int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id)
140 {
141         struct device *dev = &cl->dev;
142         const struct ti_lmu_data *data;
143         struct regmap_config regmap_cfg;
144         struct ti_lmu *lmu;
145         int ret;
146
147         /*
148          * Get device specific data from of_match table.
149          * This data is defined by using TI_LMU_DATA() macro.
150          */
151         data = of_device_get_match_data(dev);
152         if (!data)
153                 return -ENODEV;
154
155         lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL);
156         if (!lmu)
157                 return -ENOMEM;
158
159         lmu->dev = &cl->dev;
160
161         /* Setup regmap */
162         memset(&regmap_cfg, 0, sizeof(struct regmap_config));
163         regmap_cfg.reg_bits = 8;
164         regmap_cfg.val_bits = 8;
165         regmap_cfg.name = id->name;
166         regmap_cfg.max_register = data->max_register;
167
168         lmu->regmap = devm_regmap_init_i2c(cl, &regmap_cfg);
169         if (IS_ERR(lmu->regmap))
170                 return PTR_ERR(lmu->regmap);
171
172         /* HW enable pin control and additional power up sequence if required */
173         lmu->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
174         if (IS_ERR(lmu->en_gpio)) {
175                 ret = PTR_ERR(lmu->en_gpio);
176                 dev_err(dev, "Can not request enable GPIO: %d\n", ret);
177                 return ret;
178         }
179
180         ret = ti_lmu_enable_hw(lmu, id->driver_data);
181         if (ret)
182                 return ret;
183
184         ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu);
185         if (ret)
186                 return ret;
187
188         /*
189          * Fault circuit(open/short) can be detected by ti-lmu-fault-monitor.
190          * After fault detection is done, some devices should re-initialize
191          * configuration. The notifier enables such kind of handling.
192          */
193         BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier);
194
195         i2c_set_clientdata(cl, lmu);
196
197         return devm_mfd_add_devices(lmu->dev, 0, data->cells,
198                                     data->num_cells, NULL, 0, NULL);
199 }
200
201 static const struct of_device_id ti_lmu_of_match[] = {
202         { .compatible = "ti,lm3631", .data = &lm3631_data },
203         { .compatible = "ti,lm3632", .data = &lm3632_data },
204         { .compatible = "ti,lm3633", .data = &lm3633_data },
205         { .compatible = "ti,lm3695", .data = &lm3695_data },
206         { .compatible = "ti,lm36274", .data = &lm36274_data },
207         { }
208 };
209 MODULE_DEVICE_TABLE(of, ti_lmu_of_match);
210
211 static const struct i2c_device_id ti_lmu_ids[] = {
212         { "lm3631", LM3631 },
213         { "lm3632", LM3632 },
214         { "lm3633", LM3633 },
215         { "lm3695", LM3695 },
216         { "lm36274", LM36274 },
217         { }
218 };
219 MODULE_DEVICE_TABLE(i2c, ti_lmu_ids);
220
221 static struct i2c_driver ti_lmu_driver = {
222         .probe = ti_lmu_probe,
223         .driver = {
224                 .name = "ti-lmu",
225                 .of_match_table = ti_lmu_of_match,
226         },
227         .id_table = ti_lmu_ids,
228 };
229
230 module_i2c_driver(ti_lmu_driver);
231
232 MODULE_DESCRIPTION("TI LMU MFD Core Driver");
233 MODULE_AUTHOR("Milo Kim");
234 MODULE_LICENSE("GPL v2");