blob: 5f68ff11a2985bb6519d0b5d4f2a8c1e89ce9e05 [file] [log] [blame]
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +02001/*
2 * Regulator driver for National Semiconductors LP3971 PMIC chip
3 *
4 * Copyright (C) 2009 Samsung Electronics
5 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
6 *
7 * Based on wm8350.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#include <linux/bug.h>
16#include <linux/err.h>
17#include <linux/i2c.h>
18#include <linux/kernel.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040019#include <linux/module.h>
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +020020#include <linux/regulator/driver.h>
21#include <linux/regulator/lp3971.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +020023
24struct lp3971 {
25 struct device *dev;
26 struct mutex io_lock;
27 struct i2c_client *i2c;
28 int num_regulators;
29 struct regulator_dev **rdev;
30};
31
32static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg);
33static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val);
34
35#define LP3971_SYS_CONTROL1_REG 0x07
36
37/* System control register 1 initial value,
38 bits 4 and 5 are EPROM programmable */
39#define SYS_CONTROL1_INIT_VAL 0x40
40#define SYS_CONTROL1_INIT_MASK 0xCF
41
42#define LP3971_BUCK_VOL_ENABLE_REG 0x10
43#define LP3971_BUCK_VOL_CHANGE_REG 0x20
44
45/* Voltage control registers shift:
46 LP3971_BUCK1 -> 0
47 LP3971_BUCK2 -> 4
48 LP3971_BUCK3 -> 6
49*/
Axel Lin451a73c2010-03-11 09:50:07 +080050#define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01))
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +020051#define BUCK_VOL_CHANGE_FLAG_GO 0x01
52#define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
53#define BUCK_VOL_CHANGE_FLAG_MASK 0x03
54
55#define LP3971_BUCK1_BASE 0x23
56#define LP3971_BUCK2_BASE 0x29
57#define LP3971_BUCK3_BASE 0x32
58
Tobias Klauser6faa7e02009-12-23 14:13:17 +010059static const int buck_base_addr[] = {
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +020060 LP3971_BUCK1_BASE,
61 LP3971_BUCK2_BASE,
62 LP3971_BUCK3_BASE,
63};
64
65#define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
66#define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
67
Axel Lincad8d762012-05-20 13:30:28 +080068static const unsigned int buck_voltage_map[] = {
69 0, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
70 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
71 1550000, 1600000, 1650000, 1700000, 1800000, 1900000, 2500000, 2800000,
72 3000000, 3300000,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +020073};
74
75#define BUCK_TARGET_VOL_MASK 0x3f
76#define BUCK_TARGET_VOL_MIN_IDX 0x01
77#define BUCK_TARGET_VOL_MAX_IDX 0x19
78
79#define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
80
81#define LP3971_LDO_ENABLE_REG 0x12
82#define LP3971_LDO_VOL_CONTR_BASE 0x39
83
84/* Voltage control registers:
85 LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
86 LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
87 LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
88 LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
89 LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
90*/
91#define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
92
93/* Voltage control registers shift:
94 LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
95 LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
96 LP3971_LDO5 -> 0
97*/
98#define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
99#define LDO_VOL_CONTR_MASK 0x0f
100
Axel Lincad8d762012-05-20 13:30:28 +0800101static const unsigned int ldo45_voltage_map[] = {
102 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000,
103 1400000, 1500000, 1800000, 1900000, 2500000, 2800000, 3000000, 3300000,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200104};
105
Axel Lincad8d762012-05-20 13:30:28 +0800106static const unsigned int ldo123_voltage_map[] = {
107 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
108 2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200109};
110
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200111#define LDO_VOL_MIN_IDX 0x00
112#define LDO_VOL_MAX_IDX 0x0f
113
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200114static int lp3971_ldo_is_enabled(struct regulator_dev *dev)
115{
116 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
117 int ldo = rdev_get_id(dev) - LP3971_LDO1;
118 u16 mask = 1 << (1 + ldo);
119 u16 val;
120
121 val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG);
122 return (val & mask) != 0;
123}
124
125static int lp3971_ldo_enable(struct regulator_dev *dev)
126{
127 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
128 int ldo = rdev_get_id(dev) - LP3971_LDO1;
129 u16 mask = 1 << (1 + ldo);
130
131 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask);
132}
133
134static int lp3971_ldo_disable(struct regulator_dev *dev)
135{
136 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
137 int ldo = rdev_get_id(dev) - LP3971_LDO1;
138 u16 mask = 1 << (1 + ldo);
139
140 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0);
141}
142
143static int lp3971_ldo_get_voltage(struct regulator_dev *dev)
144{
145 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
146 int ldo = rdev_get_id(dev) - LP3971_LDO1;
147 u16 val, reg;
148
149 reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo));
150 val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK;
151
Axel Lincad8d762012-05-20 13:30:28 +0800152 return dev->desc->volt_table[val];
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200153}
154
Axel Lindd8e2312012-03-23 10:49:54 +0800155static int lp3971_ldo_set_voltage_sel(struct regulator_dev *dev,
156 unsigned int selector)
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200157{
158 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
159 int ldo = rdev_get_id(dev) - LP3971_LDO1;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000160
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200161 return lp3971_set_bits(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo),
Axel Lincdb868f2010-03-09 16:53:59 +0800162 LDO_VOL_CONTR_MASK << LDO_VOL_CONTR_SHIFT(ldo),
Axel Lindd8e2312012-03-23 10:49:54 +0800163 selector << LDO_VOL_CONTR_SHIFT(ldo));
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200164}
165
166static struct regulator_ops lp3971_ldo_ops = {
Axel Lincad8d762012-05-20 13:30:28 +0800167 .list_voltage = regulator_list_voltage_table,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200168 .is_enabled = lp3971_ldo_is_enabled,
169 .enable = lp3971_ldo_enable,
170 .disable = lp3971_ldo_disable,
171 .get_voltage = lp3971_ldo_get_voltage,
Axel Lindd8e2312012-03-23 10:49:54 +0800172 .set_voltage_sel = lp3971_ldo_set_voltage_sel,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200173};
174
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200175static int lp3971_dcdc_is_enabled(struct regulator_dev *dev)
176{
177 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
178 int buck = rdev_get_id(dev) - LP3971_DCDC1;
179 u16 mask = 1 << (buck * 2);
180 u16 val;
181
182 val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG);
183 return (val & mask) != 0;
184}
185
186static int lp3971_dcdc_enable(struct regulator_dev *dev)
187{
188 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
189 int buck = rdev_get_id(dev) - LP3971_DCDC1;
190 u16 mask = 1 << (buck * 2);
191
192 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask);
193}
194
195static int lp3971_dcdc_disable(struct regulator_dev *dev)
196{
197 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
198 int buck = rdev_get_id(dev) - LP3971_DCDC1;
199 u16 mask = 1 << (buck * 2);
200
201 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0);
202}
203
204static int lp3971_dcdc_get_voltage(struct regulator_dev *dev)
205{
206 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
207 int buck = rdev_get_id(dev) - LP3971_DCDC1;
208 u16 reg;
209 int val;
210
211 reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck));
212 reg &= BUCK_TARGET_VOL_MASK;
213
214 if (reg <= BUCK_TARGET_VOL_MAX_IDX)
Axel Lincad8d762012-05-20 13:30:28 +0800215 val = buck_voltage_map[reg];
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200216 else {
217 val = 0;
218 dev_warn(&dev->dev, "chip reported incorrect voltage value.\n");
219 }
220
221 return val;
222}
223
Axel Lindd8e2312012-03-23 10:49:54 +0800224static int lp3971_dcdc_set_voltage_sel(struct regulator_dev *dev,
225 unsigned int selector)
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200226{
227 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
228 int buck = rdev_get_id(dev) - LP3971_DCDC1;
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200229 int ret;
230
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200231 ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck),
Axel Lindd8e2312012-03-23 10:49:54 +0800232 BUCK_TARGET_VOL_MASK, selector);
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200233 if (ret)
234 return ret;
235
236 ret = lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
237 BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
238 BUCK_VOL_CHANGE_FLAG_GO << BUCK_VOL_CHANGE_SHIFT(buck));
239 if (ret)
240 return ret;
241
242 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
243 BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
244 0 << BUCK_VOL_CHANGE_SHIFT(buck));
245}
246
247static struct regulator_ops lp3971_dcdc_ops = {
Axel Lincad8d762012-05-20 13:30:28 +0800248 .list_voltage = regulator_list_voltage_table,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200249 .is_enabled = lp3971_dcdc_is_enabled,
250 .enable = lp3971_dcdc_enable,
251 .disable = lp3971_dcdc_disable,
252 .get_voltage = lp3971_dcdc_get_voltage,
Axel Lindd8e2312012-03-23 10:49:54 +0800253 .set_voltage_sel = lp3971_dcdc_set_voltage_sel,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200254};
255
Axel Lin14add4f2012-04-05 11:59:20 +0800256static const struct regulator_desc regulators[] = {
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200257 {
258 .name = "LDO1",
259 .id = LP3971_LDO1,
260 .ops = &lp3971_ldo_ops,
261 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
Axel Lincad8d762012-05-20 13:30:28 +0800262 .volt_table = ldo123_voltage_map,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200263 .type = REGULATOR_VOLTAGE,
264 .owner = THIS_MODULE,
265 },
266 {
267 .name = "LDO2",
268 .id = LP3971_LDO2,
269 .ops = &lp3971_ldo_ops,
270 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
Axel Lincad8d762012-05-20 13:30:28 +0800271 .volt_table = ldo123_voltage_map,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200272 .type = REGULATOR_VOLTAGE,
273 .owner = THIS_MODULE,
274 },
275 {
276 .name = "LDO3",
277 .id = LP3971_LDO3,
278 .ops = &lp3971_ldo_ops,
279 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
Axel Lincad8d762012-05-20 13:30:28 +0800280 .volt_table = ldo123_voltage_map,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200281 .type = REGULATOR_VOLTAGE,
282 .owner = THIS_MODULE,
283 },
284 {
285 .name = "LDO4",
286 .id = LP3971_LDO4,
287 .ops = &lp3971_ldo_ops,
288 .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
Axel Lincad8d762012-05-20 13:30:28 +0800289 .volt_table = ldo45_voltage_map,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200290 .type = REGULATOR_VOLTAGE,
291 .owner = THIS_MODULE,
292 },
293 {
294 .name = "LDO5",
295 .id = LP3971_LDO5,
296 .ops = &lp3971_ldo_ops,
297 .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
Axel Lincad8d762012-05-20 13:30:28 +0800298 .volt_table = ldo45_voltage_map,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200299 .type = REGULATOR_VOLTAGE,
300 .owner = THIS_MODULE,
301 },
302 {
303 .name = "DCDC1",
304 .id = LP3971_DCDC1,
305 .ops = &lp3971_dcdc_ops,
306 .n_voltages = ARRAY_SIZE(buck_voltage_map),
Axel Lincad8d762012-05-20 13:30:28 +0800307 .volt_table = buck_voltage_map,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200308 .type = REGULATOR_VOLTAGE,
309 .owner = THIS_MODULE,
310 },
311 {
312 .name = "DCDC2",
313 .id = LP3971_DCDC2,
314 .ops = &lp3971_dcdc_ops,
315 .n_voltages = ARRAY_SIZE(buck_voltage_map),
Axel Lincad8d762012-05-20 13:30:28 +0800316 .volt_table = buck_voltage_map,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200317 .type = REGULATOR_VOLTAGE,
318 .owner = THIS_MODULE,
319 },
320 {
321 .name = "DCDC3",
322 .id = LP3971_DCDC3,
323 .ops = &lp3971_dcdc_ops,
324 .n_voltages = ARRAY_SIZE(buck_voltage_map),
Axel Lincad8d762012-05-20 13:30:28 +0800325 .volt_table = buck_voltage_map,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200326 .type = REGULATOR_VOLTAGE,
327 .owner = THIS_MODULE,
328 },
329};
330
331static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count,
332 u16 *dest)
333{
334 int ret;
335
336 if (count != 1)
337 return -EIO;
338 ret = i2c_smbus_read_byte_data(i2c, reg);
Axel Lin27ef7f02010-08-06 08:09:14 +0800339 if (ret < 0)
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200340 return -EIO;
341
342 *dest = ret;
343 return 0;
344}
345
346static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count,
347 const u16 *src)
348{
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200349 if (count != 1)
350 return -EIO;
Axel Lin1bddc2f2010-08-09 17:01:21 +0800351 return i2c_smbus_write_byte_data(i2c, reg, *src);
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200352}
353
354static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg)
355{
356 u16 val = 0;
357
358 mutex_lock(&lp3971->io_lock);
359
360 lp3971_i2c_read(lp3971->i2c, reg, 1, &val);
361
362 dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
363 (unsigned)val&0xff);
364
365 mutex_unlock(&lp3971->io_lock);
366
367 return val & 0xff;
368}
369
370static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val)
371{
372 u16 tmp;
373 int ret;
374
375 mutex_lock(&lp3971->io_lock);
376
377 ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp);
378 tmp = (tmp & ~mask) | val;
379 if (ret == 0) {
380 ret = lp3971_i2c_write(lp3971->i2c, reg, 1, &tmp);
381 dev_dbg(lp3971->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
382 (unsigned)val&0xff);
383 }
384 mutex_unlock(&lp3971->io_lock);
385
386 return ret;
387}
388
Bill Pembertona5023572012-11-19 13:22:22 -0500389static int setup_regulators(struct lp3971 *lp3971,
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800390 struct lp3971_platform_data *pdata)
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200391{
392 int i, err;
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800393
394 lp3971->num_regulators = pdata->num_regulators;
395 lp3971->rdev = kcalloc(pdata->num_regulators,
396 sizeof(struct regulator_dev *), GFP_KERNEL);
Dan Carpenter67e46f32010-03-07 15:36:45 +0300397 if (!lp3971->rdev) {
398 err = -ENOMEM;
399 goto err_nomem;
400 }
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200401
402 /* Instantiate the regulators */
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800403 for (i = 0; i < pdata->num_regulators; i++) {
Mark Brownc1727082012-04-04 00:50:22 +0100404 struct regulator_config config = { };
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800405 struct lp3971_regulator_subdev *reg = &pdata->regulators[i];
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200406
Mark Brownc1727082012-04-04 00:50:22 +0100407 config.dev = lp3971->dev;
408 config.init_data = reg->initdata;
409 config.driver_data = lp3971;
410
411 lp3971->rdev[i] = regulator_register(&regulators[reg->id],
412 &config);
Julia Lawalld662fc82009-11-21 22:18:44 +0100413 if (IS_ERR(lp3971->rdev[i])) {
414 err = PTR_ERR(lp3971->rdev[i]);
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200415 dev_err(lp3971->dev, "regulator init failed: %d\n",
416 err);
417 goto error;
418 }
419 }
420
421 return 0;
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800422
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200423error:
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800424 while (--i >= 0)
425 regulator_unregister(lp3971->rdev[i]);
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200426 kfree(lp3971->rdev);
427 lp3971->rdev = NULL;
Dan Carpenter67e46f32010-03-07 15:36:45 +0300428err_nomem:
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200429 return err;
430}
431
Bill Pembertona5023572012-11-19 13:22:22 -0500432static int lp3971_i2c_probe(struct i2c_client *i2c,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200433 const struct i2c_device_id *id)
434{
435 struct lp3971 *lp3971;
436 struct lp3971_platform_data *pdata = i2c->dev.platform_data;
437 int ret;
438 u16 val;
439
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800440 if (!pdata) {
441 dev_dbg(&i2c->dev, "No platform init data supplied\n");
442 return -ENODEV;
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200443 }
444
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800445 lp3971 = kzalloc(sizeof(struct lp3971), GFP_KERNEL);
446 if (lp3971 == NULL)
447 return -ENOMEM;
448
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200449 lp3971->i2c = i2c;
450 lp3971->dev = &i2c->dev;
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200451
452 mutex_init(&lp3971->io_lock);
453
454 /* Detect LP3971 */
455 ret = lp3971_i2c_read(i2c, LP3971_SYS_CONTROL1_REG, 1, &val);
456 if (ret == 0 && (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL)
457 ret = -ENODEV;
458 if (ret < 0) {
459 dev_err(&i2c->dev, "failed to detect device\n");
460 goto err_detect;
461 }
462
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800463 ret = setup_regulators(lp3971, pdata);
464 if (ret < 0)
465 goto err_detect;
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200466
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800467 i2c_set_clientdata(i2c, lp3971);
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200468 return 0;
469
470err_detect:
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200471 kfree(lp3971);
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200472 return ret;
473}
474
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500475static int lp3971_i2c_remove(struct i2c_client *i2c)
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200476{
477 struct lp3971 *lp3971 = i2c_get_clientdata(i2c);
478 int i;
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800479
Dmitry Torokhovebbed042010-02-23 23:38:17 -0800480 for (i = 0; i < lp3971->num_regulators; i++)
481 regulator_unregister(lp3971->rdev[i]);
482
483 kfree(lp3971->rdev);
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200484 kfree(lp3971);
485
486 return 0;
487}
488
489static const struct i2c_device_id lp3971_i2c_id[] = {
490 { "lp3971", 0 },
491 { }
492};
493MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id);
494
495static struct i2c_driver lp3971_i2c_driver = {
496 .driver = {
497 .name = "LP3971",
498 .owner = THIS_MODULE,
499 },
500 .probe = lp3971_i2c_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500501 .remove = lp3971_i2c_remove,
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200502 .id_table = lp3971_i2c_id,
503};
504
Mark Brown5af34e62012-04-03 23:33:10 +0100505module_i2c_driver(lp3971_i2c_driver);
Marek Szyprowski0cbdf7b2009-05-19 07:33:55 +0200506
507MODULE_LICENSE("GPL");
508MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
509MODULE_DESCRIPTION("LP3971 PMIC driver");