LP8758 - consolidated version
[mlp-open-source/kernel.git] / drivers / regulator / lp8758.c
1 /*
2 * Simple driver for Texas Instruments lp8758 Regulator chip
3 *
4 * Copyright (C) 2015 Texas Instruments
5 * Author: Daniel Jeong  <daniel.jeong@ti.com>
6 *         Ldd Mlp <ldd-mlp@list.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 */
14 #include <linux/err.h>
15 #include <linux/gpio.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_gpio.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/lp8758.h>
24 #include <linux/regulator/machine.h>
25 #include <linux/regulator/of_regulator.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
29 struct lp8758_chip {
30         struct device *dev;
31         struct regmap *regmap;
32         struct lp8758_platform_data *pdata;
34         struct regulator_dev *rdev[LP8758_BUCK_MAX];
35         struct regulator_desc regulators[LP8758_BUCK_MAX];
36 };
38 /* voltage map */
39 static const unsigned int lp8758_buck_voltage_map[] = {
40         /* 0.50V - 0.73V : 10mV steps */
41          500000,  510000,  520000,  530000,  540000,  550000,  560000,  570000,
42          580000,  590000,  600000,  610000,  620000,  630000,  640000,  650000,
43          660000,  670000,  680000,  690000,  700000,  710000,  720000,  730000,
44         /* 0.73V - 1.40V :  5mV steps */
45          735000,  740000,  745000,  750000,  755000,  760000,  765000,  770000,
46          775000,  780000,  785000,  790000,  795000,  800000,  805000,  810000,
47          815000,  820000,  825000,  830000,  835000,  840000,  845000,  850000,
48          855000,  860000,  865000,  870000,  875000,  880000,  885000,  890000, 
49          895000,  900000,  905000,  910000,  915000,  920000,  925000,  930000,
50          935000,  940000,  945000,  950000,  955000,  960000,  965000,  970000,
51          975000,  980000,  985000,  990000,  995000, 1000000, 1005000, 1010000,
52         1015000, 1020000, 1025000, 1030000, 1035000, 1040000, 1045000, 1050000,
53         1055000, 1060000, 1065000, 1070000, 1075000, 1080000, 1085000, 1090000,
54         1095000, 1100000, 1105000, 1110000, 1115000, 1120000, 1125000, 1130000,
55         1135000, 1140000, 1145000, 1150000,     1155000, 1160000, 1165000, 1170000,
56         1175000, 1180000, 1185000, 1190000, 1195000, 1200000, 1205000, 1210000,
57         1215000, 1220000, 1225000, 1230000, 1235000, 1240000, 1245000, 1250000,
58         1255000, 1260000, 1265000, 1270000, 1275000, 1280000, 1285000, 1290000,
59         1295000, 1300000, 1305000, 1310000, 1315000, 1320000, 1325000, 1330000,
60         1335000, 1340000, 1345000, 1350000,     1355000, 1360000, 1365000, 1370000,
61         1375000, 1380000, 1385000, 1390000, 1395000, 1400000,
62         /* 1.40V - 3.36V : 20mV steps */
63         1420000, 1440000, 1460000, 1480000, 1500000, 1520000, 1540000, 1560000,
64         1580000, 1600000, 1620000, 1640000, 1660000, 1680000, 1700000, 1720000,
65         1740000, 1760000, 1780000, 1800000, 1820000, 1840000, 1860000, 1880000,
66         1900000, 1920000, 1940000, 1960000, 1980000, 2000000, 2020000, 2040000,
67         2060000, 2080000, 2100000, 2120000, 2140000, 2160000, 2180000, 2200000,
68         2220000, 2240000, 2260000, 2280000, 2300000, 2320000, 2340000, 2360000,
69         2380000, 2400000, 2420000, 2440000, 2460000, 2480000, 2500000, 2520000,
70         2540000, 2560000, 2580000, 2600000, 2620000, 2640000, 2660000, 2680000,
71         2700000, 2720000, 2740000, 2760000, 2780000, 2800000, 2820000, 2840000,
72         2860000, 2880000, 2900000, 2920000, 2940000, 2960000, 2980000, 3000000,
73         3020000, 3040000, 3060000, 3080000, 3100000, 3120000, 3140000, 3160000,
74         3180000, 3200000, 3220000, 3240000, 3260000, 3280000, 3300000, 3320000,
75         3340000, 3360000,
76 };
78 /* current limit */
79 static const unsigned int lp8758_current_limit_uA[] = {
80         1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 4500000, 5000000
81 };
83 const static int sub_version_config[LP8758_SUB_VER_MAX][LP8758_BUCK_MAX] ={
84         [LP8758_SUB_VER_B0] = { LP8758_BUCK_MASTER, LP8758_BUCK_SLAVE,
85                                                         LP8758_BUCK_SLAVE, LP8758_BUCK_SLAVE },
86         [LP8758_SUB_VER_D0] = { LP8758_BUCK_MASTER, LP8758_BUCK_SLAVE,
87                                                         LP8758_BUCK_MASTER, LP8758_BUCK_MASTER },
88         [LP8758_SUB_VER_E0] = { LP8758_BUCK_MASTER, LP8758_BUCK_MASTER,
89                                                         LP8758_BUCK_MASTER, LP8758_BUCK_MASTER },
90         [LP8758_SUB_VER_F0] = { LP8758_BUCK_MASTER, LP8758_BUCK_SLAVE,
91                                                         LP8758_BUCK_MASTER, LP8758_BUCK_SLAVE }
92 };
94 static bool lp8758_is_master(struct lp8758_chip *pchip, enum lp8758_bucks buck) {
95         int ver = pchip->pdata->sub_version;
96         if(sub_version_config[ver][buck] == LP8758_BUCK_MASTER)
97                 return true;
98         return false;
99 }
101 static unsigned int lp8758_get_ctrl1_address(enum lp8758_bucks buck){
102         unsigned int ctrl_register[LP8758_BUCK_MAX] = {
103                 [LP8758_BUCK0] = LP8758_REG_BUCK0_CTRL1,
104                 [LP8758_BUCK1] = LP8758_REG_BUCK1_CTRL1,
105                 [LP8758_BUCK2] = LP8758_REG_BUCK2_CTRL1,
106                 [LP8758_BUCK3] = LP8758_REG_BUCK3_CTRL1
107         };
108         return ctrl_register[buck];
111 static unsigned int lp8758_get_ctrl2_address(enum lp8758_bucks buck){
112         unsigned int ctrl_register[LP8758_BUCK_MAX] = {
113                 [LP8758_BUCK0] = LP8758_REG_BUCK0_CTRL2,
114                 [LP8758_BUCK1] = LP8758_REG_BUCK1_CTRL2,
115                 [LP8758_BUCK2] = LP8758_REG_BUCK2_CTRL2,
116                 [LP8758_BUCK3] = LP8758_REG_BUCK3_CTRL2
117         };
118         return ctrl_register[buck];
121 static int lp8758_buck_set_current_limit(struct regulator_dev *rdev,
122                                         int min_uA, int max_uA)
124         struct lp8758_chip *pchip = rdev_get_drvdata(rdev);
125         enum lp8758_bucks buck = rdev_get_id(rdev);
126         int icnt;
127         
128         if(buck > LP8758_BUCK_MAX-1)
129                 return -EINVAL;
131         for (icnt = ARRAY_SIZE(lp8758_current_limit_uA) - 1; icnt >= 0; icnt--) {
132                 if (lp8758_current_limit_uA[icnt] >= min_uA &&
133                         lp8758_current_limit_uA[icnt] <= max_uA){
134                                 return regmap_update_bits(pchip->regmap,
135                                                         lp8758_get_ctrl2_address(buck),
136                                                         LP8758_ILIM_MASK, icnt << LP8758_ILIM_SHIFT);
137                         }
138         }
140         return -EINVAL;
143 static int lp8758_buck_get_current_limit(struct regulator_dev *rdev)
145         struct lp8758_chip *pchip = rdev_get_drvdata(rdev);
146         enum lp8758_bucks buck = rdev_get_id(rdev);
147         unsigned int val;
148         int ret;
149         
150         if(buck > LP8758_BUCK_MAX-1)
151                 return -EINVAL;
153         ret = regmap_read(pchip->regmap, lp8758_get_ctrl2_address(buck), &val);
154         if (ret)
155                 return ret;
157         val = (val & LP8758_ILIM_MASK) >> LP8758_ILIM_SHIFT;
159         return (val < ARRAY_SIZE(lp8758_current_limit_uA)) ?
160                         lp8758_current_limit_uA[val] : -EINVAL;
163 static int lp8758_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
165         struct lp8758_chip *pchip = rdev_get_drvdata(rdev);
166         enum lp8758_bucks buck = rdev_get_id(rdev);
167         unsigned int addr;
169         if(buck > LP8758_BUCK_MAX-1)
170                 return -EINVAL;
172         addr = lp8758_get_ctrl1_address(buck);
173         if (mode == REGULATOR_MODE_FAST)
174                 return regmap_update_bits(pchip->regmap, addr, 
175                                                                         LP8758_BUCK_OP_MODE_MASK,
176                                                                         LP8758_BUCK_OP_FPWM);
177         else if (mode == REGULATOR_MODE_NORMAL)
178                 return regmap_update_bits(pchip->regmap, addr,
179                                                                         LP8758_BUCK_OP_MODE_MASK,
180                                                                         LP8758_BUCK_OP_AUTO);
181         else
182                 return -EINVAL;
185 static unsigned int lp8758_buck_get_mode(struct regulator_dev *rdev)
187         struct lp8758_chip *pchip = rdev_get_drvdata(rdev);
188         enum lp8758_bucks buck = rdev_get_id(rdev);
189         unsigned int val;
190         int ret;
192         if(buck > LP8758_BUCK_MAX-1)
193                 return -EINVAL;
195         ret = regmap_read(pchip->regmap, lp8758_get_ctrl1_address(buck), &val);
196         if (ret)
197                 return ret;
199         return (val & LP8758_BUCK_OP_MODE_MASK) 
200                                         ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
203 static int lp8758_buck_set_ramp(struct regulator_dev *rdev, int ramp)
205         int ret = -EINVAL;
206         unsigned int regval = 0x00;
207         enum lp8758_bucks buck = rdev_get_id(rdev);
208         struct lp8758_chip *pchip = rdev_get_drvdata(rdev);
210         if(buck > LP8758_BUCK_MAX-1)
211                 return -EINVAL;
213         if(lp8758_is_master(pchip,buck)) {
214                 /* uV/us */
215                 switch (ramp) {
216                 case 0 ... 470:
217                         regval = 0x07;
218                         break;
219                 case 471 ... 940:
220                         regval = 0x06;
221                         break;
222                 case 941 ... 1900:
223                         regval = 0x05;
224                         break;
225                 case 1901 ... 3800:
226                         regval = 0x04;
227                         break;
228                 case 3801 ... 7500:
229                         regval = 0x03;
230                         break;
231                 case 7501 ... 10000:
232                         regval = 0x02;
233                         break;
234                 case 10001 ... 15000:
235                         regval = 0x01;
236                         break;
237                 case 15001 ... 30000:
238                         regval = 0x00;
239                         break;
240                 default:
241                         dev_err(pchip->dev,
242                                 "Not supported ramp value %d %s\n", ramp, __func__);
243                         return -EINVAL;
244                 }
245                 ret = regmap_update_bits(pchip->regmap, lp8758_get_ctrl2_address(buck),
246                                                                         LP8758_BUCK_RAMP_MASK, regval);
247         }
248         return ret;
251 /* regulator operation when it is set to master */
252 static struct regulator_ops lp8758_buck_master_ops = {
253         .map_voltage = regulator_map_voltage_ascend,
254         .list_voltage = regulator_list_voltage_table,
255         .set_voltage_sel = regulator_set_voltage_sel_regmap,
256         .get_voltage_sel = regulator_get_voltage_sel_regmap,
257         .enable = regulator_enable_regmap,
258         .disable = regulator_disable_regmap,
259         .is_enabled = regulator_is_enabled_regmap,
260         .set_mode = lp8758_buck_set_mode,
261         .get_mode = lp8758_buck_get_mode,
262         .set_current_limit = lp8758_buck_set_current_limit,
263         .get_current_limit = lp8758_buck_get_current_limit,
264         .set_ramp_delay = lp8758_buck_set_ramp,
265 };
267 /* regulator operation when it is tied to another as slave */
268 static struct regulator_ops lp8758_buck_slave_ops = {
269         .set_current_limit = lp8758_buck_set_current_limit,
270         .get_current_limit = lp8758_buck_get_current_limit,
271 };
273 #define lp8758_rail(_id) "lp8755-buck"#_id
274 /* regulator description when it is set to master */
275 #define lp8758_buck_master_desc(_id)\
276 {\
277         .name = lp8758_rail(_id),\
278         .id   = LP8758_BUCK##_id,\
279         .ops  = &lp8758_buck_master_ops,\
280         .n_voltages = ARRAY_SIZE(lp8758_buck_voltage_map),\
281         .volt_table = lp8758_buck_voltage_map,\
282         .type = REGULATOR_VOLTAGE,\
283         .owner = THIS_MODULE,\
284         .enable_reg = LP8758_REG_BUCK##_id##_CTRL1,\
285         .enable_mask = LP8758_BUCK_EN_MASK,\
286         .vsel_reg = LP8758_REG_BUCK##_id##_VOUT,\
287         .vsel_mask = LP8758_BUCK_VOUT_MASK,\
290 /* regulator description when it is set to master and roof/floor control */
291 #define lp8758_buck_master_roof_floor_desc(_id)\
292 {\
293         .name = lp8758_rail(_id),\
294         .id   = LP8758_BUCK##_id,\
295         .ops  = &lp8758_buck_master_ops,\
296         .n_voltages = ARRAY_SIZE(lp8758_buck_voltage_map),\
297         .volt_table = lp8758_buck_voltage_map,\
298         .type = REGULATOR_VOLTAGE,\
299         .owner = THIS_MODULE,\
300         .enable_reg = LP8758_REG_BUCK##_id##_CTRL1,\
301         .enable_mask = LP8758_BUCK_EN_MASK,\
302         .vsel_reg = LP8758_REG_BUCK##_id##_FLOORVOUT,\
303         .vsel_mask = LP8758_BUCK_VOUT_MASK,\
306 /* regulator description when it is tied to another as slave */
307 #define lp8758_buck_slave_desc(_id)\
308 {\
309         .name = lp8758_rail(_id),\
310         .id   = LP8758_BUCK##_id,\
311         .ops  = &lp8758_buck_slave_ops,\
312         .type = REGULATOR_CURRENT,\
313         .owner = THIS_MODULE,\
316 /* regulators description for all configuration  */
317 static struct regulator_desc lp8758_buck_master_config[LP8758_BUCK_MAX][LP8758_CTRL_MAX] = {
318         {
319                 [LP8758_CTRL_MODE0] = lp8758_buck_master_desc(0),
320                 [LP8758_CTRL_MODE1] = lp8758_buck_master_desc(0),
321                 [LP8758_CTRL_MODE2] = lp8758_buck_master_roof_floor_desc(0),
322                 [LP8758_CTRL_MODE3] = lp8758_buck_master_desc(0),
323                 [LP8758_CTRL_MODE4] = lp8758_buck_master_roof_floor_desc(0)
324         },
325         {
326                 [LP8758_CTRL_MODE0] = lp8758_buck_master_desc(1),
327                 [LP8758_CTRL_MODE1] = lp8758_buck_master_desc(1),
328                 [LP8758_CTRL_MODE2] = lp8758_buck_master_roof_floor_desc(1),
329                 [LP8758_CTRL_MODE3] = lp8758_buck_master_desc(1),
330                 [LP8758_CTRL_MODE4] = lp8758_buck_master_roof_floor_desc(1)
331         },
332         {
333                 [LP8758_CTRL_MODE0] = lp8758_buck_master_desc(2),
334                 [LP8758_CTRL_MODE1] = lp8758_buck_master_desc(2),
335                 [LP8758_CTRL_MODE2] = lp8758_buck_master_roof_floor_desc(2),
336                 [LP8758_CTRL_MODE3] = lp8758_buck_master_desc(2),
337                 [LP8758_CTRL_MODE4] = lp8758_buck_master_roof_floor_desc(2)
338         },
339         {
340                 [LP8758_CTRL_MODE0] = lp8758_buck_master_desc(3),
341                 [LP8758_CTRL_MODE1] = lp8758_buck_master_desc(3),
342                 [LP8758_CTRL_MODE2] = lp8758_buck_master_roof_floor_desc(3),
343                 [LP8758_CTRL_MODE3] = lp8758_buck_master_desc(3),
344                 [LP8758_CTRL_MODE4] = lp8758_buck_master_roof_floor_desc(3)
345         }
346 };
348 static struct regulator_desc lp8758_buck_slave_config[LP8758_BUCK_MAX] = {
349         lp8758_buck_slave_desc(0),
350         lp8758_buck_slave_desc(1),
351         lp8758_buck_slave_desc(2),
352         lp8758_buck_slave_desc(3)
353 };
355 /*
356  * select regulator description for each buck
357  * and write configuration value into control register 
358  */
359 static int lp8758_regulator_init(struct lp8758_chip *pchip){
360         int icnt, ret, bctrl_mode;
361         struct regulator_desc *reg;
363         if(pchip->pdata->sub_version > LP8758_SUB_VER_MAX-1)
364                 return -EINVAL;
366         /* select regulator description based on sub version*/
367         for(icnt = LP8758_BUCK0 ; icnt < LP8758_BUCK_MAX; icnt++){
368                 /* select regulator description based on sub version & control mode */
369                 if(lp8758_is_master(pchip,icnt)) {
370                         bctrl_mode = pchip->pdata->buck_ctrl[icnt];
371                         if(bctrl_mode > LP8758_CTRL_MAX-1)
372                                 return -EINVAL;
373                         reg = &lp8758_buck_master_config[icnt][bctrl_mode];
375                         /* control registser set */
376                         if(bctrl_mode != LP8758_CTRL_MODE0)
377                                 bctrl_mode = (bctrl_mode - 1) | LP8758_CTRL_PIN_EN_MASK;
379                         ret = regmap_update_bits(pchip->regmap, lp8758_get_ctrl1_address(icnt),
380                                                                         LP8758_CTRL_BUCK_MASK,
381                                                                         bctrl_mode << LP8758_CTRL_BUCK_SHIFT);
382                         if(ret < 0){
383                                 dev_err(pchip->dev, "lp8758 %s i2c error \n",__func__);
384                                 return ret;
385                         }
386                 } else {
387                         reg = &lp8758_buck_slave_config[icnt],
388                         dev_err(pchip->dev, "lp8758 %d regulator is SLAVE\n",icnt);
389                 }
390                 memcpy(&pchip->regulators[icnt], reg, sizeof(struct regulator_desc));
391         }
392         return 0;
395 static int lp8758_regulator_register(struct lp8758_chip *pchip)
397         int ret, icnt;
398         struct lp8758_platform_data *pdata = pchip->pdata;
399         struct regulator_config rconfig = { };
401         rconfig.regmap = pchip->regmap;
402         rconfig.dev = pchip->dev;
403         rconfig.driver_data = pchip;
405         for(icnt = 0; icnt < LP8758_BUCK_MAX; icnt++){
406                 rconfig.init_data = pdata->buck_data[icnt];
407                 rconfig.of_node = pchip->dev->of_node;
408                 pchip->rdev[icnt] =
409                     devm_regulator_register(pchip->dev,
410                                     &pchip->regulators[icnt], &rconfig);
411                 if (IS_ERR(pchip->rdev[icnt])) {
412                         ret = PTR_ERR(pchip->rdev[icnt]);
413                         pchip->rdev[icnt] = NULL;
414                         dev_err(pchip->dev,
415                                                 "regulator init failed: buck %d\n",     icnt);
416                         return ret;
417                 }
418         }
419         return 0;
422 static irqreturn_t lp8758_irq_handler(int irq, void *data)
424         int ret, icnt;
425         unsigned int int_top, rdata;
426         struct lp8758_chip *pchip = data;
428         ret = regmap_read(pchip->regmap, LP8758_REG_INT_TOP, &int_top);
429         if(int_top & LP8758_INT_TMEP_MASK)
430                 for(icnt = LP8758_BUCK0; icnt < LP8758_BUCK_MAX; icnt++)
431                         regulator_notifier_call_chain(pchip->rdev[icnt],
432                                                                         REGULATOR_EVENT_OVER_TEMP, NULL);
434         if(int_top & LP8758_INT_BUCK01_MASK) {
435                 ret = regmap_read(pchip->regmap, LP8758_REG_INT_BUCK_01, &rdata);
436                 if(rdata & LP8758_INT_OVC_BUCK0_MASK)
437                         regulator_notifier_call_chain(pchip->rdev[LP8758_BUCK0],
438                                                                         LP8758_EVENT_OCP, NULL);
439                 if(rdata & LP8758_INT_OVC_BUCK1_MASK)
440                         regulator_notifier_call_chain(pchip->rdev[LP8758_BUCK1],
441                                                                         LP8758_EVENT_OCP, NULL);
442                 if(rdata & LP8758_INT_PWR_FAULT_BUCK0_MASK)
443                         regulator_notifier_call_chain(pchip->rdev[LP8758_BUCK0],
444                                                                         LP8758_EVENT_PWR_FAULT, NULL);
445                 if(rdata & LP8758_INT_PWR_FAULT_BUCK1_MASK)
446                         regulator_notifier_call_chain(pchip->rdev[LP8758_BUCK1],
447                                                                         LP8758_EVENT_PWR_FAULT, NULL);
448         }
449         
450         if(int_top & LP8758_INT_BUCK23_MASK) {
451                 ret = regmap_read(pchip->regmap, LP8758_REG_INT_BUCK_23, &rdata);
452                 if(rdata & LP8758_INT_OVC_BUCK2_MASK)
453                         regulator_notifier_call_chain(pchip->rdev[LP8758_BUCK2],
454                                                                         LP8758_EVENT_OCP, NULL);
455                 if(rdata & LP8758_INT_OVC_BUCK3_MASK)
456                         regulator_notifier_call_chain(pchip->rdev[LP8758_BUCK3],
457                                                                         LP8758_EVENT_OCP, NULL);
458                 if(rdata & LP8758_INT_PWR_FAULT_BUCK2_MASK)
459                         regulator_notifier_call_chain(pchip->rdev[LP8758_BUCK2],
460                                                                         LP8758_EVENT_PWR_FAULT, NULL);
461                 if(rdata & LP8758_INT_PWR_FAULT_BUCK3_MASK)
462                         regulator_notifier_call_chain(pchip->rdev[LP8758_BUCK3],
463                                                                         LP8758_EVENT_PWR_FAULT, NULL);
464         }
465         
466         /* clear interrupt */
467         regmap_write(pchip->regmap, LP8758_REG_INT_BUCK_01, LP8758_INT_CLEAR_BUCK);
468         regmap_write(pchip->regmap, LP8758_REG_INT_BUCK_23, LP8758_INT_CLEAR_BUCK);
469         regmap_write(pchip->regmap, LP8758_REG_INT_TOP, LP8758_INT_CLEAR_TOP);
471         dev_err(pchip->dev, "lp8758 IRQ Handeled");
472         return IRQ_HANDLED;
475 static int lp8758_intr_config(struct lp8758_chip *pchip)
477         int ret, irq;
479         if (pchip->pdata->irq == 0) {
480                 dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
481                 return 0;
482         }
484         /* initially clear interrupt */
485         regmap_write(pchip->regmap, LP8758_REG_INT_BUCK_01, LP8758_INT_CLEAR_BUCK);
486         regmap_write(pchip->regmap, LP8758_REG_INT_BUCK_23, LP8758_INT_CLEAR_BUCK);
487         regmap_write(pchip->regmap, LP8758_REG_INT_TOP, LP8758_INT_CLEAR_TOP);
489         gpio_request_one(pchip->pdata->irq, GPIOF_DIR_IN,"lp8758-irq");
490         irq = gpio_to_irq(pchip->pdata->irq);
491         if(irq < 0){
492                 dev_warn(pchip->dev, "irq can't be configurated\n");
493                 return -EINVAL;
494         }
496         ret = request_threaded_irq(irq, NULL, lp8758_irq_handler,
497                                    IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
498                                    "lp8758-irq", pchip);
499         return ret;
502 static struct of_regulator_match lp8758_matches[LP8758_BUCK_MAX] = {
503         { .name = "buck0", .driver_data = (void *)LP8758_BUCK0, },
504         { .name = "buck1", .driver_data = (void *)LP8758_BUCK1, },
505         { .name = "buck2", .driver_data = (void *)LP8758_BUCK2, },
506         { .name = "buck3", .driver_data = (void *)LP8758_BUCK3, },
507 };
509 static int lp8758_parse_dt(struct i2c_client *client,
510                                                                 struct lp8758_chip *pchip)
512         struct device_node *node = client->dev.of_node;
513         int err, icnt;
515         pchip->pdata = devm_kzalloc(&client->dev,
516                      sizeof(struct lp8758_platform_data), GFP_KERNEL);
517         if(pchip->pdata == NULL){
518                 dev_err(&client->dev, "lp8758 -- platform data is null\n");
519                 return -ENOMEM;
520         }
522         err = of_regulator_match(&client->dev, node,
523                                                                 lp8758_matches, LP8758_BUCK_MAX);
524         if (err <= 0){
525                 dev_err(&client->dev, "lp8758 --ERR - of regulator match\n");
526                 return -EINVAL;
527         }
529         for(icnt = 0; icnt < LP8758_BUCK_MAX; icnt++){
530                 pchip->pdata->buck_data[icnt] = lp8758_matches[icnt].init_data;
531         }
533         pchip->pdata->irq = of_get_named_gpio(node,"irq-gpio", 0);
534         err = of_property_read_u32(node, "sub_version",
535                                                                 &pchip->pdata->sub_version);
536         if(err < 0){
537                 dev_err(&client->dev, "lp8758 --ERR - of chip version read\n");
538                 return -EINVAL;
539         }
541         err = of_property_read_u32_array(node, "buck_ctrl",
542                                                                         pchip->pdata->buck_ctrl, LP8758_BUCK_MAX);
543         if(err < 0){
544                 dev_err(&client->dev, "lp8758 --ERR - pin ctrl data\n");
545                 return -EINVAL;
546         }
547         return 0;
550 static const struct regmap_config lp8758_regmap = {
551         .reg_bits = 8,
552         .val_bits = 8,
553         .max_register = LP8758_REG_MAX,
554 };
556 static const struct of_device_id of_lp8758_bl_match[] = {
557         { .compatible = "ti,lp8758", },
558         {},
559 };
561 static int lp8758_probe(struct i2c_client *client,
562                         const struct i2c_device_id *id)
564         struct lp8758_chip *pchip;
565         int ret = 0;
567         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
568                 dev_err(&client->dev, "i2c functionality check fail.\n");
569                 return -EOPNOTSUPP;
570         }
572         pchip = devm_kzalloc(&client->dev,
573                              sizeof(struct lp8758_chip), GFP_KERNEL);
574         if (!pchip)
575                 return -ENOMEM;
577         pchip->regmap = devm_regmap_init_i2c(client, &lp8758_regmap);
578         if (IS_ERR(pchip->regmap)) {
579                 ret = PTR_ERR(pchip->regmap);
580                 dev_err(&client->dev, "fail : allocate i2c register map\n");
581                 return ret;
582         }
584         ret = lp8758_parse_dt(client, pchip);
585         if(ret < 0)
586                 return ret;
587         pchip->dev = &client->dev;
588         i2c_set_clientdata(client, pchip);
590         ret = lp8758_regulator_init(pchip);
591         if (ret < 0) {
592                 dev_err(&client->dev, "fail to initialize regulators\n");
593                 return ret;
594         }
596         ret = lp8758_regulator_register(pchip);
597         if (ret < 0) {
598                 dev_err(&client->dev, "fail to register regulators\n");
599                 return ret;
600         }
602         ret = lp8758_intr_config(pchip);
603         if (ret < 0) {
604                 dev_err(&client->dev, "fail to irq config\n");
605                 return ret;
606         }
608         dev_info(&client->dev, "lp8758 probe done\n");
609         return 0;
612 static const struct i2c_device_id lp8758_id[] = {
613         {LP8758_NAME, 0},
614         {}
615 };
617 MODULE_DEVICE_TABLE(i2c, lp8758_id);
618 static struct i2c_driver lp8758_i2c_driver = {
619         .driver = {
620                 .name = LP8758_NAME,
621                 .owner   = THIS_MODULE,
622                 .of_match_table = of_match_ptr(of_lp8758_bl_match),
623         },
624         .probe = lp8758_probe,
625         .id_table = lp8758_id,
626 };
628 module_i2c_driver(lp8758_i2c_driver);
630 MODULE_DESCRIPTION("Texas Instruments lp8758 driver");
631 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
632 MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
633 MODULE_LICENSE("GPL v2");