blob: fc85a42e57d4a448d8a14c9293c437bff5f39e16 [file] [log] [blame]
David Collinsfdefdf92011-09-08 14:18:59 -07001/*
2 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/kernel.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
David Collins8c695c52011-12-12 15:46:02 -080019#include <linux/string.h>
David Collinsfdefdf92011-09-08 14:18:59 -070020#include <linux/err.h>
21#include <linux/msm_ssbi.h>
22#include <linux/mfd/core.h>
23#include <linux/mfd/pm8xxx/pm8018.h>
24#include <linux/mfd/pm8xxx/core.h>
David Collins8c695c52011-12-12 15:46:02 -080025#include <linux/mfd/pm8xxx/regulator.h>
Jay Chokshie91f55c2011-10-17 15:10:07 -070026#include <linux/leds-pm8xxx.h>
David Collinsfdefdf92011-09-08 14:18:59 -070027
28
29/* PMIC PM8018 SSBI Addresses */
30#define REG_HWREV 0x002 /* PMIC4 revision */
31#define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */
32
33#define REG_MPP_BASE 0x050
Anirudh Ghayalca42c7de2011-11-21 10:42:07 +053034#define REG_IRQ_BASE 0x1BB
David Collinsfdefdf92011-09-08 14:18:59 -070035
36#define REG_RTC_BASE 0x11D
37
38#define REG_TEMP_ALARM_CTRL 0x01B
39#define REG_TEMP_ALARM_PWM 0x09B
40
41
42#define PM8018_VERSION_MASK 0xFFF0
43#define PM8018_VERSION_VALUE 0x08F0
44#define PM8018_REVISION_MASK 0x000F
45
Jay Chokshib3c30e82011-10-17 19:11:35 -070046#define REG_PM8018_PON_CNTRL_3 0x01D
47#define PM8018_RESTART_REASON_MASK 0x07
48
David Collinsfdefdf92011-09-08 14:18:59 -070049#define SINGLE_IRQ_RESOURCE(_name, _irq) \
50{ \
51 .name = _name, \
52 .start = _irq, \
53 .end = _irq, \
54 .flags = IORESOURCE_IRQ, \
55}
56
57struct pm8018 {
David Collins8c695c52011-12-12 15:46:02 -080058 struct device *dev;
59 struct pm_irq_chip *irq_chip;
60 struct mfd_cell *mfd_regulators;
61 struct pm8xxx_regulator_core_platform_data *regulator_cdata;
62 u32 rev_registers;
David Collinsfdefdf92011-09-08 14:18:59 -070063};
64
65static int pm8018_readb(const struct device *dev, u16 addr, u8 *val)
66{
67 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
68 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
69
70 return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
71}
72
73static int pm8018_writeb(const struct device *dev, u16 addr, u8 val)
74{
75 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
76 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
77
78 return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
79}
80
81static int pm8018_read_buf(const struct device *dev, u16 addr, u8 *buf,
82 int cnt)
83{
84 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
85 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
86
87 return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
88}
89
90static int pm8018_write_buf(const struct device *dev, u16 addr, u8 *buf,
91 int cnt)
92{
93 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
94 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
95
96 return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
97}
98
99static int pm8018_read_irq_stat(const struct device *dev, int irq)
100{
101 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
102 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
103
104 return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
105}
106
107static enum pm8xxx_version pm8018_get_version(const struct device *dev)
108{
109 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
110 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
111 enum pm8xxx_version version = -ENODEV;
112
113 if ((pmic->rev_registers & PM8018_VERSION_MASK) == PM8018_VERSION_VALUE)
114 version = PM8XXX_VERSION_8018;
115
116 return version;
117}
118
119static int pm8018_get_revision(const struct device *dev)
120{
121 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
122 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
123
124 return pmic->rev_registers & PM8018_REVISION_MASK;
125}
126
127static struct pm8xxx_drvdata pm8018_drvdata = {
128 .pmic_readb = pm8018_readb,
129 .pmic_writeb = pm8018_writeb,
130 .pmic_read_buf = pm8018_read_buf,
131 .pmic_write_buf = pm8018_write_buf,
132 .pmic_read_irq_stat = pm8018_read_irq_stat,
133 .pmic_get_version = pm8018_get_version,
134 .pmic_get_revision = pm8018_get_revision,
135};
136
137static const struct resource gpio_cell_resources[] __devinitconst = {
138 [0] = {
139 .start = PM8018_IRQ_BLOCK_BIT(PM8018_GPIO_BLOCK_START, 0),
140 .end = PM8018_IRQ_BLOCK_BIT(PM8018_GPIO_BLOCK_START, 0)
141 + PM8018_NR_GPIOS - 1,
142 .flags = IORESOURCE_IRQ,
143 },
144};
145
146static struct mfd_cell gpio_cell __devinitdata = {
147 .name = PM8XXX_GPIO_DEV_NAME,
148 .id = -1,
149 .resources = gpio_cell_resources,
150 .num_resources = ARRAY_SIZE(gpio_cell_resources),
151};
152
Siddartha Mohanadoss5bcd2062011-10-06 15:51:41 -0700153static const struct resource adc_cell_resources[] __devinitconst = {
154 SINGLE_IRQ_RESOURCE(NULL, PM8018_ADC_EOC_USR_IRQ),
155 SINGLE_IRQ_RESOURCE(NULL, PM8018_ADC_BATT_TEMP_WARM_IRQ),
156 SINGLE_IRQ_RESOURCE(NULL, PM8018_ADC_BATT_TEMP_COLD_IRQ),
157};
158
159static struct mfd_cell adc_cell __devinitdata = {
160 .name = PM8XXX_ADC_DEV_NAME,
161 .id = -1,
162 .resources = adc_cell_resources,
163 .num_resources = ARRAY_SIZE(adc_cell_resources),
164};
165
David Collinsfdefdf92011-09-08 14:18:59 -0700166static const struct resource mpp_cell_resources[] __devinitconst = {
167 {
168 .start = PM8018_IRQ_BLOCK_BIT(PM8018_MPP_BLOCK_START, 0),
169 .end = PM8018_IRQ_BLOCK_BIT(PM8018_MPP_BLOCK_START, 0)
170 + PM8018_NR_MPPS - 1,
171 .flags = IORESOURCE_IRQ,
172 },
173};
174
175static struct mfd_cell mpp_cell __devinitdata = {
176 .name = PM8XXX_MPP_DEV_NAME,
177 .id = -1,
178 .resources = mpp_cell_resources,
179 .num_resources = ARRAY_SIZE(mpp_cell_resources),
180};
181
182static const struct resource rtc_cell_resources[] __devinitconst = {
183 [0] = SINGLE_IRQ_RESOURCE(NULL, PM8018_RTC_ALARM_IRQ),
184 [1] = {
185 .name = "pmic_rtc_base",
186 .start = REG_RTC_BASE,
187 .end = REG_RTC_BASE,
188 .flags = IORESOURCE_IO,
189 },
190};
191
192static struct mfd_cell rtc_cell __devinitdata = {
193 .name = PM8XXX_RTC_DEV_NAME,
194 .id = -1,
195 .resources = rtc_cell_resources,
196 .num_resources = ARRAY_SIZE(rtc_cell_resources),
197};
198
199static const struct resource resources_pwrkey[] __devinitconst = {
200 SINGLE_IRQ_RESOURCE(NULL, PM8018_PWRKEY_REL_IRQ),
201 SINGLE_IRQ_RESOURCE(NULL, PM8018_PWRKEY_PRESS_IRQ),
202};
203
204static struct mfd_cell pwrkey_cell __devinitdata = {
205 .name = PM8XXX_PWRKEY_DEV_NAME,
206 .id = -1,
207 .num_resources = ARRAY_SIZE(resources_pwrkey),
208 .resources = resources_pwrkey,
209};
210
211static struct mfd_cell misc_cell __devinitdata = {
212 .name = PM8XXX_MISC_DEV_NAME,
213 .id = -1,
214};
215
216static struct mfd_cell debugfs_cell __devinitdata = {
217 .name = "pm8xxx-debug",
218 .id = -1,
219 .platform_data = "pm8018-dbg",
220 .pdata_size = sizeof("pm8018-dbg"),
221};
222
Jay Chokshie91f55c2011-10-17 15:10:07 -0700223static struct mfd_cell pwm_cell __devinitdata = {
224 .name = PM8XXX_PWM_DEV_NAME,
225 .id = -1,
226};
227
228static struct mfd_cell leds_cell __devinitdata = {
229 .name = PM8XXX_LEDS_DEV_NAME,
230 .id = -1,
231};
232
David Collins8c695c52011-12-12 15:46:02 -0800233static struct pm8xxx_vreg regulator_data[] = {
234 /* name pc_name ctrl test hpm_min */
235 PLDO("8018_l2", "8018_l2_pc", 0x0B0, 0x0B1, LDO_50),
236 PLDO("8018_l3", "8018_l3_pc", 0x0B2, 0x0B3, LDO_50),
237 PLDO("8018_l4", "8018_l4_pc", 0x0B4, 0x0B5, LDO_300),
238 PLDO("8018_l5", "8018_l5_pc", 0x0B6, 0x0B7, LDO_150),
239 PLDO("8018_l6", "8018_l6_pc", 0x0B8, 0x0B9, LDO_150),
240 PLDO("8018_l7", "8018_l7_pc", 0x0BA, 0x0BB, LDO_300),
241 NLDO("8018_l8", "8018_l8_pc", 0x0BC, 0x0BD, LDO_150),
242 NLDO1200("8018_l9", 0x0BE, 0x0BF, LDO_1200),
243 NLDO1200("8018_l10", 0x0C0, 0x0C1, LDO_1200),
244 NLDO1200("8018_l11", 0x0C2, 0x0C3, LDO_1200),
245 NLDO1200("8018_l12", 0x0C4, 0x0C5, LDO_1200),
246 PLDO("8018_l13", "8018_l13_pc", 0x0C8, 0x0C9, LDO_50),
247 PLDO("8018_l14", "8018_l14_pc", 0x0CA, 0x0CB, LDO_50),
248
249 /* name pc_name ctrl test2 clk sleep hpm_min */
250 SMPS("8018_s1", "8018_s1_pc", 0x1D0, 0x1D5, 0x009, 0x1D2, SMPS_1500),
251 SMPS("8018_s2", "8018_s2_pc", 0x1D8, 0x1DD, 0x00A, 0x1DA, SMPS_1500),
252 SMPS("8018_s3", "8018_s3_pc", 0x1E0, 0x1E5, 0x00B, 0x1E2, SMPS_1500),
253 SMPS("8018_s4", "8018_s4_pc", 0x1E8, 0x1ED, 0x00C, 0x1EA, SMPS_1500),
254 SMPS("8018_s5", "8018_s5_pc", 0x1F0, 0x1F5, 0x00D, 0x1F2, SMPS_1500),
255
256 /* name pc_name ctrl */
257 VS("8018_lvs1", "8018_lvs1_pc", 0x060),
258};
259
260#define MAX_NAME_COMPARISON_LEN 32
261
262static int __devinit match_regulator(
263 struct pm8xxx_regulator_core_platform_data *core_data, char *name)
264{
265 int found = 0;
266 int i;
267
268 for (i = 0; i < ARRAY_SIZE(regulator_data); i++) {
269 if (regulator_data[i].rdesc.name
270 && strncmp(regulator_data[i].rdesc.name, name,
271 MAX_NAME_COMPARISON_LEN) == 0) {
272 core_data->is_pin_controlled = false;
273 core_data->vreg = &regulator_data[i];
274 found = 1;
275 break;
276 } else if (regulator_data[i].rdesc_pc.name
277 && strncmp(regulator_data[i].rdesc_pc.name, name,
278 MAX_NAME_COMPARISON_LEN) == 0) {
279 core_data->is_pin_controlled = true;
280 core_data->vreg = &regulator_data[i];
281 found = 1;
282 break;
283 }
284 }
285
286 if (!found)
287 pr_err("could not find a match for regulator: %s\n", name);
288
289 return found;
290}
291
292static int __devinit
293pm8018_add_regulators(const struct pm8018_platform_data *pdata,
294 struct pm8018 *pmic, int irq_base)
295{
296 int ret = 0;
297 struct mfd_cell *mfd_regulators;
298 struct pm8xxx_regulator_core_platform_data *cdata;
299 int i;
300
301 /* Add one device for each regulator used by the board. */
302 mfd_regulators = kzalloc(sizeof(struct mfd_cell)
303 * (pdata->num_regulators), GFP_KERNEL);
304 if (!mfd_regulators) {
305 pr_err("Cannot allocate %d bytes for pm8018 regulator "
306 "mfd cells\n", sizeof(struct mfd_cell)
307 * (pdata->num_regulators));
308 return -ENOMEM;
309 }
310 cdata = kzalloc(sizeof(struct pm8xxx_regulator_core_platform_data)
311 * pdata->num_regulators, GFP_KERNEL);
312 if (!cdata) {
313 pr_err("Cannot allocate %d bytes for pm8018 regulator "
314 "core data\n", pdata->num_regulators
315 * sizeof(struct pm8xxx_regulator_core_platform_data));
316 kfree(mfd_regulators);
317 return -ENOMEM;
318 }
319 for (i = 0; i < ARRAY_SIZE(regulator_data); i++)
320 mutex_init(&regulator_data[i].pc_lock);
321
322 for (i = 0; i < pdata->num_regulators; i++) {
323 if (!pdata->regulator_pdatas[i].init_data.constraints.name) {
324 pr_err("name missing for regulator %d\n", i);
325 ret = -EINVAL;
326 goto bail;
327 }
328 if (!match_regulator(&cdata[i],
329 pdata->regulator_pdatas[i].init_data.constraints.name)) {
330 ret = -ENODEV;
331 goto bail;
332 }
333 cdata[i].pdata = &(pdata->regulator_pdatas[i]);
334 mfd_regulators[i].name = PM8XXX_REGULATOR_DEV_NAME;
335 mfd_regulators[i].id = cdata[i].pdata->id;
336 mfd_regulators[i].platform_data = &cdata[i];
337 mfd_regulators[i].pdata_size =
338 sizeof(struct pm8xxx_regulator_core_platform_data);
339 }
340 ret = mfd_add_devices(pmic->dev, 0, mfd_regulators,
341 pdata->num_regulators, NULL, irq_base);
342 if (ret)
343 goto bail;
344
345 pmic->mfd_regulators = mfd_regulators;
346 pmic->regulator_cdata = cdata;
347 return ret;
348
349bail:
350 for (i = 0; i < ARRAY_SIZE(regulator_data); i++)
351 mutex_destroy(&regulator_data[i].pc_lock);
352 kfree(mfd_regulators);
353 kfree(cdata);
354 return ret;
355}
356
David Collinsfdefdf92011-09-08 14:18:59 -0700357static int __devinit
358pm8018_add_subdevices(const struct pm8018_platform_data *pdata,
359 struct pm8018 *pmic)
360{
361 int ret = 0, irq_base = 0;
362 struct pm_irq_chip *irq_chip;
363
364 if (pdata->irq_pdata) {
365 pdata->irq_pdata->irq_cdata.nirqs = PM8018_NR_IRQS;
Anirudh Ghayalca42c7de2011-11-21 10:42:07 +0530366 pdata->irq_pdata->irq_cdata.base_addr = REG_IRQ_BASE;
David Collinsfdefdf92011-09-08 14:18:59 -0700367 irq_base = pdata->irq_pdata->irq_base;
368 irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
369
370 if (IS_ERR(irq_chip)) {
371 pr_err("Failed to init interrupts ret=%ld\n",
372 PTR_ERR(irq_chip));
373 return PTR_ERR(irq_chip);
374 }
375 pmic->irq_chip = irq_chip;
376 }
377
378 if (pdata->gpio_pdata) {
379 pdata->gpio_pdata->gpio_cdata.ngpios = PM8018_NR_GPIOS;
380 gpio_cell.platform_data = pdata->gpio_pdata;
381 gpio_cell.pdata_size = sizeof(struct pm8xxx_gpio_platform_data);
382 ret = mfd_add_devices(pmic->dev, 0, &gpio_cell, 1,
383 NULL, irq_base);
384 if (ret) {
385 pr_err("Failed to add gpio subdevice ret=%d\n", ret);
386 goto bail;
387 }
388 }
389
390 if (pdata->mpp_pdata) {
391 pdata->mpp_pdata->core_data.nmpps = PM8018_NR_MPPS;
392 pdata->mpp_pdata->core_data.base_addr = REG_MPP_BASE;
393 mpp_cell.platform_data = pdata->mpp_pdata;
394 mpp_cell.pdata_size = sizeof(struct pm8xxx_mpp_platform_data);
395 ret = mfd_add_devices(pmic->dev, 0, &mpp_cell, 1, NULL,
396 irq_base);
397 if (ret) {
398 pr_err("Failed to add mpp subdevice ret=%d\n", ret);
399 goto bail;
400 }
401 }
402
403 if (pdata->rtc_pdata) {
404 rtc_cell.platform_data = pdata->rtc_pdata;
405 rtc_cell.pdata_size = sizeof(struct pm8xxx_rtc_platform_data);
406 ret = mfd_add_devices(pmic->dev, 0, &rtc_cell, 1, NULL,
407 irq_base);
408 if (ret) {
409 pr_err("Failed to add rtc subdevice ret=%d\n", ret);
410 goto bail;
411 }
412 }
413
414 if (pdata->pwrkey_pdata) {
415 pwrkey_cell.platform_data = pdata->pwrkey_pdata;
416 pwrkey_cell.pdata_size =
417 sizeof(struct pm8xxx_pwrkey_platform_data);
418 ret = mfd_add_devices(pmic->dev, 0, &pwrkey_cell, 1, NULL,
419 irq_base);
420 if (ret) {
421 pr_err("Failed to add pwrkey subdevice ret=%d\n", ret);
422 goto bail;
423 }
424 }
425
426 if (pdata->misc_pdata) {
427 misc_cell.platform_data = pdata->misc_pdata;
428 misc_cell.pdata_size = sizeof(struct pm8xxx_misc_platform_data);
429 ret = mfd_add_devices(pmic->dev, 0, &misc_cell, 1, NULL,
430 irq_base);
431 if (ret) {
432 pr_err("Failed to add misc subdevice ret=%d\n", ret);
433 goto bail;
434 }
435 }
436
Siddartha Mohanadoss5bcd2062011-10-06 15:51:41 -0700437 if (pdata->adc_pdata) {
438 adc_cell.platform_data = pdata->adc_pdata;
439 adc_cell.pdata_size = sizeof(struct pm8xxx_adc_platform_data);
440 ret = mfd_add_devices(pmic->dev, 0, &adc_cell, 1, NULL,
441 irq_base);
442 if (ret) {
443 pr_err("Failed to add adc subdevice ret=%d\n", ret);
Jay Chokshie91f55c2011-10-17 15:10:07 -0700444 }
445 }
446
447 if (pdata->leds_pdata) {
448 leds_cell.platform_data = pdata->leds_pdata;
449 leds_cell.pdata_size = sizeof(struct pm8xxx_led_platform_data);
450 ret = mfd_add_devices(pmic->dev, 0, &leds_cell, 1, NULL, 0);
451 if (ret) {
452 pr_err("Failed to add leds subdevice ret=%d\n", ret);
Siddartha Mohanadoss5bcd2062011-10-06 15:51:41 -0700453 goto bail;
454 }
455 }
456
David Collinsfdefdf92011-09-08 14:18:59 -0700457 ret = mfd_add_devices(pmic->dev, 0, &debugfs_cell, 1, NULL, irq_base);
458 if (ret) {
459 pr_err("Failed to add debugfs subdevice ret=%d\n", ret);
460 goto bail;
461 }
462
Jay Chokshie91f55c2011-10-17 15:10:07 -0700463 ret = mfd_add_devices(pmic->dev, 0, &pwm_cell, 1, NULL, 0);
464 if (ret) {
465 pr_err("Failed to add pwm subdevice ret=%d\n", ret);
466 goto bail;
467 }
468
David Collinsfdefdf92011-09-08 14:18:59 -0700469 if (pdata->num_regulators > 0 && pdata->regulator_pdatas) {
David Collins8c695c52011-12-12 15:46:02 -0800470 ret = pm8018_add_regulators(pdata, pmic, irq_base);
David Collinsfdefdf92011-09-08 14:18:59 -0700471 if (ret) {
472 pr_err("Failed to add regulator subdevices ret=%d\n",
473 ret);
David Collinsfdefdf92011-09-08 14:18:59 -0700474 goto bail;
475 }
David Collinsfdefdf92011-09-08 14:18:59 -0700476 }
477
David Collins8c695c52011-12-12 15:46:02 -0800478
David Collinsfdefdf92011-09-08 14:18:59 -0700479 return 0;
480bail:
481 if (pmic->irq_chip) {
482 pm8xxx_irq_exit(pmic->irq_chip);
483 pmic->irq_chip = NULL;
484 }
485 return ret;
486}
487
Jay Chokshib3c30e82011-10-17 19:11:35 -0700488static const char * const pm8018_restart_reason[] = {
489 [0] = "Unknown",
490 [1] = "Triggered from CBL (external charger)",
491 [2] = "Triggered from KPD (power key press)",
492 [3] = "Triggered from CHG (usb charger insertion)",
493 [4] = "Triggered from SMPL (sudden momentary power loss)",
494 [5] = "Triggered from RTC (real time clock)",
495 [6] = "Triggered by Hard Reset",
496 [7] = "Triggered by General Purpose Trigger",
497};
498
David Collinsfdefdf92011-09-08 14:18:59 -0700499static const char * const pm8018_rev_names[] = {
500 [PM8XXX_REVISION_8018_TEST] = "test",
501 [PM8XXX_REVISION_8018_1p0] = "1.0",
502 [PM8XXX_REVISION_8018_1p1] = "1.1",
503 [PM8XXX_REVISION_8018_2p0] = "2.0",
504};
505
506static int __devinit pm8018_probe(struct platform_device *pdev)
507{
508 const struct pm8018_platform_data *pdata = pdev->dev.platform_data;
509 const char *revision_name = "unknown";
510 struct pm8018 *pmic;
511 enum pm8xxx_version version;
512 int revision;
513 int rc;
514 u8 val;
515
516 if (!pdata) {
517 pr_err("missing platform data\n");
518 return -EINVAL;
519 }
520
521 pmic = kzalloc(sizeof(struct pm8018), GFP_KERNEL);
522 if (!pmic) {
523 pr_err("Cannot alloc pm8018 struct\n");
524 return -ENOMEM;
525 }
526
527 /* Read PMIC chip revision */
528 rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV, &val, sizeof(val));
529 if (rc) {
530 pr_err("Failed to read hw rev 1 reg %d:rc=%d\n", REG_HWREV, rc);
531 goto err_read_rev;
532 }
533 pr_info("PMIC revision 1: %02X\n", val);
534 pmic->rev_registers = val;
535
536 /* Read PMIC chip revision 2 */
537 rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV_2, &val, sizeof(val));
538 if (rc) {
539 pr_err("Failed to read hw rev 2 reg %d:rc=%d\n", REG_HWREV_2,
540 rc);
541 goto err_read_rev;
542 }
543 pr_info("PMIC revision 2: %02X\n", val);
544 pmic->rev_registers |= val << BITS_PER_BYTE;
545
546 pmic->dev = &pdev->dev;
547 pm8018_drvdata.pm_chip_data = pmic;
548 platform_set_drvdata(pdev, &pm8018_drvdata);
549
550 /* Print out human readable version and revision names. */
551 version = pm8xxx_get_version(pmic->dev);
552 if (version == PM8XXX_VERSION_8018) {
553 revision = pm8xxx_get_revision(pmic->dev);
554 if (revision >= 0 && revision < ARRAY_SIZE(pm8018_rev_names))
555 revision_name = pm8018_rev_names[revision];
556 pr_info("PMIC version: PM8018 rev %s\n", revision_name);
557 } else {
558 WARN_ON(version != PM8XXX_VERSION_8018);
559 }
Jay Chokshib3c30e82011-10-17 19:11:35 -0700560 /* Log human readable restart reason */
561 rc = msm_ssbi_read(pdev->dev.parent, REG_PM8018_PON_CNTRL_3, &val, 1);
562 if (rc) {
563 pr_err("Cannot read restart reason rc=%d\n", rc);
564 goto err_read_rev;
565 }
566 val &= PM8018_RESTART_REASON_MASK;
567 pr_info("PMIC Restart Reason: %s\n", pm8018_restart_reason[val]);
David Collinsfdefdf92011-09-08 14:18:59 -0700568
569 rc = pm8018_add_subdevices(pdata, pmic);
570 if (rc) {
571 pr_err("Cannot add subdevices rc=%d\n", rc);
572 goto err;
573 }
574
575 /* gpio might not work if no irq device is found */
576 WARN_ON(pmic->irq_chip == NULL);
577
578 return 0;
579
580err:
581 mfd_remove_devices(pmic->dev);
582 platform_set_drvdata(pdev, NULL);
David Collins8c695c52011-12-12 15:46:02 -0800583 kfree(pmic->mfd_regulators);
584 kfree(pmic->regulator_cdata);
David Collinsfdefdf92011-09-08 14:18:59 -0700585err_read_rev:
586 kfree(pmic);
587 return rc;
588}
589
590static int __devexit pm8018_remove(struct platform_device *pdev)
591{
592 struct pm8xxx_drvdata *drvdata;
593 struct pm8018 *pmic = NULL;
David Collins8c695c52011-12-12 15:46:02 -0800594 int i;
David Collinsfdefdf92011-09-08 14:18:59 -0700595
596 drvdata = platform_get_drvdata(pdev);
597 if (drvdata)
598 pmic = drvdata->pm_chip_data;
David Collins8c695c52011-12-12 15:46:02 -0800599 if (pmic) {
600 if (pmic->dev)
601 mfd_remove_devices(pmic->dev);
602 if (pmic->irq_chip) {
603 pm8xxx_irq_exit(pmic->irq_chip);
604 pmic->irq_chip = NULL;
605 }
606 if (pmic->mfd_regulators) {
607 for (i = 0; i < ARRAY_SIZE(regulator_data); i++)
608 mutex_destroy(&regulator_data[i].pc_lock);
609 }
610 kfree(pmic->mfd_regulators);
611 kfree(pmic->regulator_cdata);
612 kfree(pmic);
David Collinsfdefdf92011-09-08 14:18:59 -0700613 }
614 platform_set_drvdata(pdev, NULL);
David Collinsfdefdf92011-09-08 14:18:59 -0700615
616 return 0;
617}
618
619static struct platform_driver pm8018_driver = {
620 .probe = pm8018_probe,
621 .remove = __devexit_p(pm8018_remove),
622 .driver = {
623 .name = PM8018_CORE_DEV_NAME,
624 .owner = THIS_MODULE,
625 },
626};
627
628static int __init pm8018_init(void)
629{
630 return platform_driver_register(&pm8018_driver);
631}
632postcore_initcall(pm8018_init);
633
634static void __exit pm8018_exit(void)
635{
636 platform_driver_unregister(&pm8018_driver);
637}
638module_exit(pm8018_exit);
639
640MODULE_LICENSE("GPL v2");
641MODULE_DESCRIPTION("PMIC 8018 core driver");
642MODULE_VERSION("1.0");
643MODULE_ALIAS("platform:" PM8018_CORE_DEV_NAME);