blob: fd6e8c417baa33922756c95b561a9fb008562111 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Milo Kimd5aa11b2017-02-28 15:45:15 +09002/*
3 * TI LMU (Lighting Management Unit) Core Driver
4 *
5 * Copyright 2017 Texas Instruments
6 *
7 * Author: Milo Kim <milo.kim@ti.com>
Milo Kimd5aa11b2017-02-28 15:45:15 +09008 */
9
10#include <linux/delay.h>
11#include <linux/err.h>
Pavel Machek7a6a3952018-09-11 23:01:33 +020012#include <linux/gpio/consumer.h>
Milo Kimd5aa11b2017-02-28 15:45:15 +090013#include <linux/i2c.h>
14#include <linux/kernel.h>
15#include <linux/mfd/core.h>
16#include <linux/mfd/ti-lmu.h>
17#include <linux/mfd/ti-lmu-register.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
Milo Kimd5aa11b2017-02-28 15:45:15 +090021#include <linux/slab.h>
22
23struct ti_lmu_data {
Sebastian Reichel5b6850f2018-08-29 11:31:09 +020024 const struct mfd_cell *cells;
Milo Kimd5aa11b2017-02-28 15:45:15 +090025 int num_cells;
26 unsigned int max_register;
27};
28
29static int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id)
30{
Pavel Machek7a6a3952018-09-11 23:01:33 +020031 if (lmu->en_gpio)
32 gpiod_set_value(lmu->en_gpio, 1);
Milo Kimd5aa11b2017-02-28 15:45:15 +090033
34 /* Delay about 1ms after HW enable pin control */
35 usleep_range(1000, 1500);
36
37 /* LM3631 has additional power up sequence - enable LCD_EN bit. */
38 if (id == LM3631) {
39 return regmap_update_bits(lmu->regmap, LM3631_REG_DEVCTRL,
40 LM3631_LCD_EN_MASK,
41 LM3631_LCD_EN_MASK);
42 }
43
44 return 0;
45}
46
Pavel Machek7891d372018-09-11 23:03:44 +020047static void ti_lmu_disable_hw(void *data)
Milo Kimd5aa11b2017-02-28 15:45:15 +090048{
Pavel Machek7891d372018-09-11 23:03:44 +020049 struct ti_lmu *lmu = data;
Pavel Machek7a6a3952018-09-11 23:01:33 +020050 if (lmu->en_gpio)
51 gpiod_set_value(lmu->en_gpio, 0);
Milo Kimd5aa11b2017-02-28 15:45:15 +090052}
53
Milo Kimd5aa11b2017-02-28 15:45:15 +090054#define LM363X_REGULATOR(_id) \
55{ \
56 .name = "lm363x-regulator", \
57 .id = _id, \
58 .of_compatible = "ti,lm363x-regulator", \
59} \
60
Sebastian Reichel5b6850f2018-08-29 11:31:09 +020061static const struct mfd_cell lm3631_devices[] = {
Milo Kimd5aa11b2017-02-28 15:45:15 +090062 LM363X_REGULATOR(LM3631_BOOST),
63 LM363X_REGULATOR(LM3631_LDO_CONT),
64 LM363X_REGULATOR(LM3631_LDO_OREF),
65 LM363X_REGULATOR(LM3631_LDO_POS),
66 LM363X_REGULATOR(LM3631_LDO_NEG),
67 {
68 .name = "ti-lmu-backlight",
69 .id = LM3631,
70 .of_compatible = "ti,lm3631-backlight",
71 },
72};
73
Sebastian Reichel5b6850f2018-08-29 11:31:09 +020074static const struct mfd_cell lm3632_devices[] = {
Milo Kimd5aa11b2017-02-28 15:45:15 +090075 LM363X_REGULATOR(LM3632_BOOST),
76 LM363X_REGULATOR(LM3632_LDO_POS),
77 LM363X_REGULATOR(LM3632_LDO_NEG),
78 {
79 .name = "ti-lmu-backlight",
80 .id = LM3632,
81 .of_compatible = "ti,lm3632-backlight",
82 },
83};
84
Sebastian Reichel5b6850f2018-08-29 11:31:09 +020085static const struct mfd_cell lm3633_devices[] = {
Milo Kimd5aa11b2017-02-28 15:45:15 +090086 {
87 .name = "ti-lmu-backlight",
88 .id = LM3633,
89 .of_compatible = "ti,lm3633-backlight",
90 },
91 {
92 .name = "lm3633-leds",
93 .of_compatible = "ti,lm3633-leds",
94 },
95 /* Monitoring driver for open/short circuit detection */
96 {
97 .name = "ti-lmu-fault-monitor",
98 .id = LM3633,
99 .of_compatible = "ti,lm3633-fault-monitor",
100 },
101};
102
Sebastian Reichel5b6850f2018-08-29 11:31:09 +0200103static const struct mfd_cell lm3695_devices[] = {
Milo Kimd5aa11b2017-02-28 15:45:15 +0900104 {
105 .name = "ti-lmu-backlight",
106 .id = LM3695,
107 .of_compatible = "ti,lm3695-backlight",
108 },
109};
110
Dan Murphy9c523ef2019-06-05 07:56:31 -0500111static const struct mfd_cell lm36274_devices[] = {
112 LM363X_REGULATOR(LM36274_BOOST),
113 LM363X_REGULATOR(LM36274_LDO_POS),
114 LM363X_REGULATOR(LM36274_LDO_NEG),
Milo Kimd5aa11b2017-02-28 15:45:15 +0900115 {
Dan Murphy9c523ef2019-06-05 07:56:31 -0500116 .name = "lm36274-leds",
117 .id = LM36274,
118 .of_compatible = "ti,lm36274-backlight",
Milo Kimd5aa11b2017-02-28 15:45:15 +0900119 },
120};
121
122#define TI_LMU_DATA(chip, max_reg) \
123static const struct ti_lmu_data chip##_data = \
124{ \
125 .cells = chip##_devices, \
126 .num_cells = ARRAY_SIZE(chip##_devices),\
127 .max_register = max_reg, \
128} \
129
Milo Kimd5aa11b2017-02-28 15:45:15 +0900130TI_LMU_DATA(lm3631, LM3631_MAX_REG);
131TI_LMU_DATA(lm3632, LM3632_MAX_REG);
132TI_LMU_DATA(lm3633, LM3633_MAX_REG);
133TI_LMU_DATA(lm3695, LM3695_MAX_REG);
Dan Murphy9c523ef2019-06-05 07:56:31 -0500134TI_LMU_DATA(lm36274, LM36274_MAX_REG);
Milo Kimd5aa11b2017-02-28 15:45:15 +0900135
Milo Kimd5aa11b2017-02-28 15:45:15 +0900136static int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id)
137{
138 struct device *dev = &cl->dev;
Milo Kimd5aa11b2017-02-28 15:45:15 +0900139 const struct ti_lmu_data *data;
140 struct regmap_config regmap_cfg;
141 struct ti_lmu *lmu;
142 int ret;
143
Milo Kimd5aa11b2017-02-28 15:45:15 +0900144 /*
145 * Get device specific data from of_match table.
146 * This data is defined by using TI_LMU_DATA() macro.
147 */
Pavel Machek697894b2018-09-11 23:06:50 +0200148 data = of_device_get_match_data(dev);
149 if (!data)
150 return -ENODEV;
Milo Kimd5aa11b2017-02-28 15:45:15 +0900151
152 lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL);
153 if (!lmu)
154 return -ENOMEM;
155
156 lmu->dev = &cl->dev;
157
158 /* Setup regmap */
159 memset(&regmap_cfg, 0, sizeof(struct regmap_config));
160 regmap_cfg.reg_bits = 8;
161 regmap_cfg.val_bits = 8;
162 regmap_cfg.name = id->name;
163 regmap_cfg.max_register = data->max_register;
164
165 lmu->regmap = devm_regmap_init_i2c(cl, &regmap_cfg);
166 if (IS_ERR(lmu->regmap))
167 return PTR_ERR(lmu->regmap);
168
169 /* HW enable pin control and additional power up sequence if required */
Pavel Machek7a6a3952018-09-11 23:01:33 +0200170 lmu->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
171 if (IS_ERR(lmu->en_gpio)) {
172 ret = PTR_ERR(lmu->en_gpio);
173 dev_err(dev, "Can not request enable GPIO: %d\n", ret);
174 return ret;
175 }
176
Milo Kimd5aa11b2017-02-28 15:45:15 +0900177 ret = ti_lmu_enable_hw(lmu, id->driver_data);
178 if (ret)
179 return ret;
180
Pavel Machek7891d372018-09-11 23:03:44 +0200181 ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu);
182 if (ret)
183 return ret;
184
Milo Kimd5aa11b2017-02-28 15:45:15 +0900185 /*
186 * Fault circuit(open/short) can be detected by ti-lmu-fault-monitor.
187 * After fault detection is done, some devices should re-initialize
188 * configuration. The notifier enables such kind of handling.
189 */
190 BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier);
191
192 i2c_set_clientdata(cl, lmu);
193
Pavel Machek7891d372018-09-11 23:03:44 +0200194 return devm_mfd_add_devices(lmu->dev, 0, data->cells,
195 data->num_cells, NULL, 0, NULL);
Milo Kimd5aa11b2017-02-28 15:45:15 +0900196}
197
Pavel Machek697894b2018-09-11 23:06:50 +0200198static const struct of_device_id ti_lmu_of_match[] = {
Pavel Machek697894b2018-09-11 23:06:50 +0200199 { .compatible = "ti,lm3631", .data = &lm3631_data },
200 { .compatible = "ti,lm3632", .data = &lm3632_data },
201 { .compatible = "ti,lm3633", .data = &lm3633_data },
202 { .compatible = "ti,lm3695", .data = &lm3695_data },
Dan Murphy9c523ef2019-06-05 07:56:31 -0500203 { .compatible = "ti,lm36274", .data = &lm36274_data },
Pavel Machek697894b2018-09-11 23:06:50 +0200204 { }
205};
206MODULE_DEVICE_TABLE(of, ti_lmu_of_match);
207
Milo Kimd5aa11b2017-02-28 15:45:15 +0900208static const struct i2c_device_id ti_lmu_ids[] = {
Milo Kimd5aa11b2017-02-28 15:45:15 +0900209 { "lm3631", LM3631 },
210 { "lm3632", LM3632 },
211 { "lm3633", LM3633 },
212 { "lm3695", LM3695 },
Dan Murphy9c523ef2019-06-05 07:56:31 -0500213 { "lm36274", LM36274 },
Milo Kimd5aa11b2017-02-28 15:45:15 +0900214 { }
215};
216MODULE_DEVICE_TABLE(i2c, ti_lmu_ids);
217
218static struct i2c_driver ti_lmu_driver = {
219 .probe = ti_lmu_probe,
Milo Kimd5aa11b2017-02-28 15:45:15 +0900220 .driver = {
221 .name = "ti-lmu",
222 .of_match_table = ti_lmu_of_match,
223 },
224 .id_table = ti_lmu_ids,
225};
226
227module_i2c_driver(ti_lmu_driver);
228
229MODULE_DESCRIPTION("TI LMU MFD Core Driver");
230MODULE_AUTHOR("Milo Kim");
231MODULE_LICENSE("GPL v2");