blob: ab457388ed81a99fff4654847a00b9b642291217 [file] [log] [blame]
David Collinsfdefdf92011-09-08 14:18:59 -07001/*
Duy Truong790f06d2013-02-13 16:38:12 -08002 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
David Collinsfdefdf92011-09-08 14:18:59 -07003 *
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>
Steve Mucklef132c6c2012-06-06 18:30:57 -070017#include <linux/module.h>
David Collinsfdefdf92011-09-08 14:18:59 -070018#include <linux/platform_device.h>
19#include <linux/slab.h>
David Collins8c695c52011-12-12 15:46:02 -080020#include <linux/string.h>
David Collinsfdefdf92011-09-08 14:18:59 -070021#include <linux/err.h>
22#include <linux/msm_ssbi.h>
23#include <linux/mfd/core.h>
24#include <linux/mfd/pm8xxx/pm8018.h>
25#include <linux/mfd/pm8xxx/core.h>
David Collins8c695c52011-12-12 15:46:02 -080026#include <linux/mfd/pm8xxx/regulator.h>
Jay Chokshie91f55c2011-10-17 15:10:07 -070027#include <linux/leds-pm8xxx.h>
David Collinsfdefdf92011-09-08 14:18:59 -070028
29
30/* PMIC PM8018 SSBI Addresses */
31#define REG_HWREV 0x002 /* PMIC4 revision */
32#define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */
33
34#define REG_MPP_BASE 0x050
Anirudh Ghayalca42c7de2011-11-21 10:42:07 +053035#define REG_IRQ_BASE 0x1BB
David Collinsfdefdf92011-09-08 14:18:59 -070036
37#define REG_RTC_BASE 0x11D
38
39#define REG_TEMP_ALARM_CTRL 0x01B
40#define REG_TEMP_ALARM_PWM 0x09B
41
42
43#define PM8018_VERSION_MASK 0xFFF0
44#define PM8018_VERSION_VALUE 0x08F0
45#define PM8018_REVISION_MASK 0x000F
46
Jay Chokshib3c30e82011-10-17 19:11:35 -070047#define REG_PM8018_PON_CNTRL_3 0x01D
Jay Chokshib3c30e82011-10-17 19:11:35 -070048
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;
Abhijeet Dharmapurikara4ed55c2012-08-28 15:07:43 -070063 u8 restart_reason;
David Collinsfdefdf92011-09-08 14:18:59 -070064};
65
66static int pm8018_readb(const struct device *dev, u16 addr, u8 *val)
67{
68 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
69 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
70
71 return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
72}
73
74static int pm8018_writeb(const struct device *dev, u16 addr, u8 val)
75{
76 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
77 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
78
79 return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
80}
81
82static int pm8018_read_buf(const struct device *dev, u16 addr, u8 *buf,
83 int cnt)
84{
85 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
86 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
87
88 return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
89}
90
91static int pm8018_write_buf(const struct device *dev, u16 addr, u8 *buf,
92 int cnt)
93{
94 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
95 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
96
97 return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
98}
99
100static int pm8018_read_irq_stat(const struct device *dev, int irq)
101{
102 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
103 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
104
105 return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
106}
107
108static enum pm8xxx_version pm8018_get_version(const struct device *dev)
109{
110 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
111 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
112 enum pm8xxx_version version = -ENODEV;
113
114 if ((pmic->rev_registers & PM8018_VERSION_MASK) == PM8018_VERSION_VALUE)
115 version = PM8XXX_VERSION_8018;
116
117 return version;
118}
119
120static int pm8018_get_revision(const struct device *dev)
121{
122 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
123 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
124
125 return pmic->rev_registers & PM8018_REVISION_MASK;
126}
127
Abhijeet Dharmapurikara4ed55c2012-08-28 15:07:43 -0700128static u8 pm8018_restart_reason(const struct device *dev)
129{
130 const struct pm8xxx_drvdata *pm8018_drvdata = dev_get_drvdata(dev);
131 const struct pm8018 *pmic = pm8018_drvdata->pm_chip_data;
132
133 return pmic->restart_reason;
134}
135
David Collinsfdefdf92011-09-08 14:18:59 -0700136static struct pm8xxx_drvdata pm8018_drvdata = {
137 .pmic_readb = pm8018_readb,
138 .pmic_writeb = pm8018_writeb,
139 .pmic_read_buf = pm8018_read_buf,
140 .pmic_write_buf = pm8018_write_buf,
141 .pmic_read_irq_stat = pm8018_read_irq_stat,
142 .pmic_get_version = pm8018_get_version,
143 .pmic_get_revision = pm8018_get_revision,
Abhijeet Dharmapurikara4ed55c2012-08-28 15:07:43 -0700144 .pmic_restart_reason = pm8018_restart_reason,
David Collinsfdefdf92011-09-08 14:18:59 -0700145};
146
147static const struct resource gpio_cell_resources[] __devinitconst = {
148 [0] = {
149 .start = PM8018_IRQ_BLOCK_BIT(PM8018_GPIO_BLOCK_START, 0),
150 .end = PM8018_IRQ_BLOCK_BIT(PM8018_GPIO_BLOCK_START, 0)
151 + PM8018_NR_GPIOS - 1,
152 .flags = IORESOURCE_IRQ,
153 },
154};
155
156static struct mfd_cell gpio_cell __devinitdata = {
157 .name = PM8XXX_GPIO_DEV_NAME,
158 .id = -1,
159 .resources = gpio_cell_resources,
160 .num_resources = ARRAY_SIZE(gpio_cell_resources),
161};
162
Siddartha Mohanadoss5bcd2062011-10-06 15:51:41 -0700163static const struct resource adc_cell_resources[] __devinitconst = {
164 SINGLE_IRQ_RESOURCE(NULL, PM8018_ADC_EOC_USR_IRQ),
165 SINGLE_IRQ_RESOURCE(NULL, PM8018_ADC_BATT_TEMP_WARM_IRQ),
166 SINGLE_IRQ_RESOURCE(NULL, PM8018_ADC_BATT_TEMP_COLD_IRQ),
167};
168
169static struct mfd_cell adc_cell __devinitdata = {
170 .name = PM8XXX_ADC_DEV_NAME,
171 .id = -1,
172 .resources = adc_cell_resources,
173 .num_resources = ARRAY_SIZE(adc_cell_resources),
174};
175
David Collinsfdefdf92011-09-08 14:18:59 -0700176static const struct resource mpp_cell_resources[] __devinitconst = {
177 {
178 .start = PM8018_IRQ_BLOCK_BIT(PM8018_MPP_BLOCK_START, 0),
179 .end = PM8018_IRQ_BLOCK_BIT(PM8018_MPP_BLOCK_START, 0)
180 + PM8018_NR_MPPS - 1,
181 .flags = IORESOURCE_IRQ,
182 },
183};
184
185static struct mfd_cell mpp_cell __devinitdata = {
186 .name = PM8XXX_MPP_DEV_NAME,
187 .id = -1,
188 .resources = mpp_cell_resources,
189 .num_resources = ARRAY_SIZE(mpp_cell_resources),
190};
191
192static const struct resource rtc_cell_resources[] __devinitconst = {
193 [0] = SINGLE_IRQ_RESOURCE(NULL, PM8018_RTC_ALARM_IRQ),
194 [1] = {
195 .name = "pmic_rtc_base",
196 .start = REG_RTC_BASE,
197 .end = REG_RTC_BASE,
198 .flags = IORESOURCE_IO,
199 },
200};
201
202static struct mfd_cell rtc_cell __devinitdata = {
203 .name = PM8XXX_RTC_DEV_NAME,
204 .id = -1,
205 .resources = rtc_cell_resources,
206 .num_resources = ARRAY_SIZE(rtc_cell_resources),
207};
208
209static const struct resource resources_pwrkey[] __devinitconst = {
210 SINGLE_IRQ_RESOURCE(NULL, PM8018_PWRKEY_REL_IRQ),
211 SINGLE_IRQ_RESOURCE(NULL, PM8018_PWRKEY_PRESS_IRQ),
212};
213
214static struct mfd_cell pwrkey_cell __devinitdata = {
215 .name = PM8XXX_PWRKEY_DEV_NAME,
216 .id = -1,
217 .num_resources = ARRAY_SIZE(resources_pwrkey),
218 .resources = resources_pwrkey,
219};
220
221static struct mfd_cell misc_cell __devinitdata = {
222 .name = PM8XXX_MISC_DEV_NAME,
223 .id = -1,
224};
225
226static struct mfd_cell debugfs_cell __devinitdata = {
227 .name = "pm8xxx-debug",
228 .id = -1,
229 .platform_data = "pm8018-dbg",
230 .pdata_size = sizeof("pm8018-dbg"),
231};
232
Jay Chokshie91f55c2011-10-17 15:10:07 -0700233static struct mfd_cell pwm_cell __devinitdata = {
234 .name = PM8XXX_PWM_DEV_NAME,
235 .id = -1,
236};
237
238static struct mfd_cell leds_cell __devinitdata = {
239 .name = PM8XXX_LEDS_DEV_NAME,
240 .id = -1,
241};
242
David Collins39914be22012-03-20 09:47:21 -0700243static const struct resource thermal_alarm_cell_resources[] __devinitconst = {
244 SINGLE_IRQ_RESOURCE("pm8018_tempstat_irq", PM8018_TEMPSTAT_IRQ),
245 SINGLE_IRQ_RESOURCE("pm8018_overtemp_irq", PM8018_OVERTEMP_IRQ),
246};
247
248static struct pm8xxx_tm_core_data thermal_alarm_cdata = {
249 .adc_channel = CHANNEL_DIE_TEMP,
250 .adc_type = PM8XXX_TM_ADC_PM8XXX_ADC,
251 .reg_addr_temp_alarm_ctrl = REG_TEMP_ALARM_CTRL,
252 .reg_addr_temp_alarm_pwm = REG_TEMP_ALARM_PWM,
253 .tm_name = "pm8018_tz",
254 .irq_name_temp_stat = "pm8018_tempstat_irq",
255 .irq_name_over_temp = "pm8018_overtemp_irq",
256};
257
258static struct mfd_cell thermal_alarm_cell __devinitdata = {
259 .name = PM8XXX_TM_DEV_NAME,
260 .id = -1,
261 .resources = thermal_alarm_cell_resources,
262 .num_resources = ARRAY_SIZE(thermal_alarm_cell_resources),
263 .platform_data = &thermal_alarm_cdata,
264 .pdata_size = sizeof(struct pm8xxx_tm_core_data),
265};
266
David Collins8c695c52011-12-12 15:46:02 -0800267static struct pm8xxx_vreg regulator_data[] = {
268 /* name pc_name ctrl test hpm_min */
269 PLDO("8018_l2", "8018_l2_pc", 0x0B0, 0x0B1, LDO_50),
270 PLDO("8018_l3", "8018_l3_pc", 0x0B2, 0x0B3, LDO_50),
271 PLDO("8018_l4", "8018_l4_pc", 0x0B4, 0x0B5, LDO_300),
272 PLDO("8018_l5", "8018_l5_pc", 0x0B6, 0x0B7, LDO_150),
273 PLDO("8018_l6", "8018_l6_pc", 0x0B8, 0x0B9, LDO_150),
274 PLDO("8018_l7", "8018_l7_pc", 0x0BA, 0x0BB, LDO_300),
275 NLDO("8018_l8", "8018_l8_pc", 0x0BC, 0x0BD, LDO_150),
276 NLDO1200("8018_l9", 0x0BE, 0x0BF, LDO_1200),
277 NLDO1200("8018_l10", 0x0C0, 0x0C1, LDO_1200),
278 NLDO1200("8018_l11", 0x0C2, 0x0C3, LDO_1200),
279 NLDO1200("8018_l12", 0x0C4, 0x0C5, LDO_1200),
280 PLDO("8018_l13", "8018_l13_pc", 0x0C8, 0x0C9, LDO_50),
281 PLDO("8018_l14", "8018_l14_pc", 0x0CA, 0x0CB, LDO_50),
282
283 /* name pc_name ctrl test2 clk sleep hpm_min */
284 SMPS("8018_s1", "8018_s1_pc", 0x1D0, 0x1D5, 0x009, 0x1D2, SMPS_1500),
285 SMPS("8018_s2", "8018_s2_pc", 0x1D8, 0x1DD, 0x00A, 0x1DA, SMPS_1500),
286 SMPS("8018_s3", "8018_s3_pc", 0x1E0, 0x1E5, 0x00B, 0x1E2, SMPS_1500),
287 SMPS("8018_s4", "8018_s4_pc", 0x1E8, 0x1ED, 0x00C, 0x1EA, SMPS_1500),
288 SMPS("8018_s5", "8018_s5_pc", 0x1F0, 0x1F5, 0x00D, 0x1F2, SMPS_1500),
289
David Collins1b570d22012-01-30 15:40:55 -0800290 /* name pc_name ctrl test */
291 VS("8018_lvs1", "8018_lvs1_pc", 0x060, 0x061),
David Collins8c695c52011-12-12 15:46:02 -0800292};
293
294#define MAX_NAME_COMPARISON_LEN 32
295
296static int __devinit match_regulator(
Mark Brown5d028cd2011-10-14 13:36:04 +0100297 struct pm8xxx_regulator_core_platform_data *core_data, const char *name)
David Collins8c695c52011-12-12 15:46:02 -0800298{
299 int found = 0;
300 int i;
301
302 for (i = 0; i < ARRAY_SIZE(regulator_data); i++) {
303 if (regulator_data[i].rdesc.name
304 && strncmp(regulator_data[i].rdesc.name, name,
305 MAX_NAME_COMPARISON_LEN) == 0) {
306 core_data->is_pin_controlled = false;
307 core_data->vreg = &regulator_data[i];
308 found = 1;
309 break;
310 } else if (regulator_data[i].rdesc_pc.name
311 && strncmp(regulator_data[i].rdesc_pc.name, name,
312 MAX_NAME_COMPARISON_LEN) == 0) {
313 core_data->is_pin_controlled = true;
314 core_data->vreg = &regulator_data[i];
315 found = 1;
316 break;
317 }
318 }
319
320 if (!found)
321 pr_err("could not find a match for regulator: %s\n", name);
322
323 return found;
324}
325
326static int __devinit
327pm8018_add_regulators(const struct pm8018_platform_data *pdata,
328 struct pm8018 *pmic, int irq_base)
329{
330 int ret = 0;
331 struct mfd_cell *mfd_regulators;
332 struct pm8xxx_regulator_core_platform_data *cdata;
333 int i;
334
335 /* Add one device for each regulator used by the board. */
336 mfd_regulators = kzalloc(sizeof(struct mfd_cell)
337 * (pdata->num_regulators), GFP_KERNEL);
338 if (!mfd_regulators) {
339 pr_err("Cannot allocate %d bytes for pm8018 regulator "
340 "mfd cells\n", sizeof(struct mfd_cell)
341 * (pdata->num_regulators));
342 return -ENOMEM;
343 }
344 cdata = kzalloc(sizeof(struct pm8xxx_regulator_core_platform_data)
345 * pdata->num_regulators, GFP_KERNEL);
346 if (!cdata) {
347 pr_err("Cannot allocate %d bytes for pm8018 regulator "
348 "core data\n", pdata->num_regulators
349 * sizeof(struct pm8xxx_regulator_core_platform_data));
350 kfree(mfd_regulators);
351 return -ENOMEM;
352 }
353 for (i = 0; i < ARRAY_SIZE(regulator_data); i++)
354 mutex_init(&regulator_data[i].pc_lock);
355
356 for (i = 0; i < pdata->num_regulators; i++) {
357 if (!pdata->regulator_pdatas[i].init_data.constraints.name) {
358 pr_err("name missing for regulator %d\n", i);
359 ret = -EINVAL;
360 goto bail;
361 }
362 if (!match_regulator(&cdata[i],
363 pdata->regulator_pdatas[i].init_data.constraints.name)) {
364 ret = -ENODEV;
365 goto bail;
366 }
367 cdata[i].pdata = &(pdata->regulator_pdatas[i]);
368 mfd_regulators[i].name = PM8XXX_REGULATOR_DEV_NAME;
369 mfd_regulators[i].id = cdata[i].pdata->id;
370 mfd_regulators[i].platform_data = &cdata[i];
371 mfd_regulators[i].pdata_size =
372 sizeof(struct pm8xxx_regulator_core_platform_data);
373 }
374 ret = mfd_add_devices(pmic->dev, 0, mfd_regulators,
375 pdata->num_regulators, NULL, irq_base);
376 if (ret)
377 goto bail;
378
379 pmic->mfd_regulators = mfd_regulators;
380 pmic->regulator_cdata = cdata;
381 return ret;
382
383bail:
384 for (i = 0; i < ARRAY_SIZE(regulator_data); i++)
385 mutex_destroy(&regulator_data[i].pc_lock);
386 kfree(mfd_regulators);
387 kfree(cdata);
388 return ret;
389}
390
David Collinsfdefdf92011-09-08 14:18:59 -0700391static int __devinit
392pm8018_add_subdevices(const struct pm8018_platform_data *pdata,
393 struct pm8018 *pmic)
394{
395 int ret = 0, irq_base = 0;
396 struct pm_irq_chip *irq_chip;
397
398 if (pdata->irq_pdata) {
399 pdata->irq_pdata->irq_cdata.nirqs = PM8018_NR_IRQS;
Anirudh Ghayalca42c7de2011-11-21 10:42:07 +0530400 pdata->irq_pdata->irq_cdata.base_addr = REG_IRQ_BASE;
David Collinsfdefdf92011-09-08 14:18:59 -0700401 irq_base = pdata->irq_pdata->irq_base;
402 irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
403
404 if (IS_ERR(irq_chip)) {
405 pr_err("Failed to init interrupts ret=%ld\n",
406 PTR_ERR(irq_chip));
407 return PTR_ERR(irq_chip);
408 }
409 pmic->irq_chip = irq_chip;
410 }
411
412 if (pdata->gpio_pdata) {
413 pdata->gpio_pdata->gpio_cdata.ngpios = PM8018_NR_GPIOS;
414 gpio_cell.platform_data = pdata->gpio_pdata;
415 gpio_cell.pdata_size = sizeof(struct pm8xxx_gpio_platform_data);
416 ret = mfd_add_devices(pmic->dev, 0, &gpio_cell, 1,
417 NULL, irq_base);
418 if (ret) {
419 pr_err("Failed to add gpio subdevice ret=%d\n", ret);
420 goto bail;
421 }
422 }
423
424 if (pdata->mpp_pdata) {
425 pdata->mpp_pdata->core_data.nmpps = PM8018_NR_MPPS;
426 pdata->mpp_pdata->core_data.base_addr = REG_MPP_BASE;
427 mpp_cell.platform_data = pdata->mpp_pdata;
428 mpp_cell.pdata_size = sizeof(struct pm8xxx_mpp_platform_data);
429 ret = mfd_add_devices(pmic->dev, 0, &mpp_cell, 1, NULL,
430 irq_base);
431 if (ret) {
432 pr_err("Failed to add mpp subdevice ret=%d\n", ret);
433 goto bail;
434 }
435 }
436
437 if (pdata->rtc_pdata) {
438 rtc_cell.platform_data = pdata->rtc_pdata;
439 rtc_cell.pdata_size = sizeof(struct pm8xxx_rtc_platform_data);
440 ret = mfd_add_devices(pmic->dev, 0, &rtc_cell, 1, NULL,
441 irq_base);
442 if (ret) {
443 pr_err("Failed to add rtc subdevice ret=%d\n", ret);
444 goto bail;
445 }
446 }
447
448 if (pdata->pwrkey_pdata) {
449 pwrkey_cell.platform_data = pdata->pwrkey_pdata;
450 pwrkey_cell.pdata_size =
451 sizeof(struct pm8xxx_pwrkey_platform_data);
452 ret = mfd_add_devices(pmic->dev, 0, &pwrkey_cell, 1, NULL,
453 irq_base);
454 if (ret) {
455 pr_err("Failed to add pwrkey subdevice ret=%d\n", ret);
456 goto bail;
457 }
458 }
459
460 if (pdata->misc_pdata) {
461 misc_cell.platform_data = pdata->misc_pdata;
462 misc_cell.pdata_size = sizeof(struct pm8xxx_misc_platform_data);
463 ret = mfd_add_devices(pmic->dev, 0, &misc_cell, 1, NULL,
464 irq_base);
465 if (ret) {
466 pr_err("Failed to add misc subdevice ret=%d\n", ret);
467 goto bail;
468 }
469 }
470
Siddartha Mohanadoss5bcd2062011-10-06 15:51:41 -0700471 if (pdata->adc_pdata) {
472 adc_cell.platform_data = pdata->adc_pdata;
473 adc_cell.pdata_size = sizeof(struct pm8xxx_adc_platform_data);
474 ret = mfd_add_devices(pmic->dev, 0, &adc_cell, 1, NULL,
475 irq_base);
476 if (ret) {
477 pr_err("Failed to add adc subdevice ret=%d\n", ret);
Jay Chokshie91f55c2011-10-17 15:10:07 -0700478 }
479 }
480
481 if (pdata->leds_pdata) {
482 leds_cell.platform_data = pdata->leds_pdata;
483 leds_cell.pdata_size = sizeof(struct pm8xxx_led_platform_data);
484 ret = mfd_add_devices(pmic->dev, 0, &leds_cell, 1, NULL, 0);
485 if (ret) {
486 pr_err("Failed to add leds subdevice ret=%d\n", ret);
Siddartha Mohanadoss5bcd2062011-10-06 15:51:41 -0700487 goto bail;
488 }
489 }
490
David Collinsfdefdf92011-09-08 14:18:59 -0700491 ret = mfd_add_devices(pmic->dev, 0, &debugfs_cell, 1, NULL, irq_base);
492 if (ret) {
493 pr_err("Failed to add debugfs subdevice ret=%d\n", ret);
494 goto bail;
495 }
496
Jay Chokshie91f55c2011-10-17 15:10:07 -0700497 ret = mfd_add_devices(pmic->dev, 0, &pwm_cell, 1, NULL, 0);
498 if (ret) {
499 pr_err("Failed to add pwm subdevice ret=%d\n", ret);
500 goto bail;
501 }
502
David Collinsfdefdf92011-09-08 14:18:59 -0700503 if (pdata->num_regulators > 0 && pdata->regulator_pdatas) {
David Collins8c695c52011-12-12 15:46:02 -0800504 ret = pm8018_add_regulators(pdata, pmic, irq_base);
David Collinsfdefdf92011-09-08 14:18:59 -0700505 if (ret) {
506 pr_err("Failed to add regulator subdevices ret=%d\n",
507 ret);
David Collinsfdefdf92011-09-08 14:18:59 -0700508 goto bail;
509 }
David Collinsfdefdf92011-09-08 14:18:59 -0700510 }
511
David Collins39914be22012-03-20 09:47:21 -0700512 ret = mfd_add_devices(pmic->dev, 0, &thermal_alarm_cell, 1, NULL,
513 irq_base);
514 if (ret) {
515 pr_err("Failed to add thermal alarm subdevice, ret=%d\n", ret);
516 goto bail;
517 }
David Collins8c695c52011-12-12 15:46:02 -0800518
David Collinsfdefdf92011-09-08 14:18:59 -0700519 return 0;
520bail:
521 if (pmic->irq_chip) {
522 pm8xxx_irq_exit(pmic->irq_chip);
523 pmic->irq_chip = NULL;
524 }
525 return ret;
526}
527
528static const char * const pm8018_rev_names[] = {
529 [PM8XXX_REVISION_8018_TEST] = "test",
530 [PM8XXX_REVISION_8018_1p0] = "1.0",
David Collinsfdefdf92011-09-08 14:18:59 -0700531 [PM8XXX_REVISION_8018_2p0] = "2.0",
Jay Chokshi1ba115d2012-04-19 15:42:41 -0700532 [PM8XXX_REVISION_8018_2p1] = "2.1",
David Collinsfdefdf92011-09-08 14:18:59 -0700533};
534
535static int __devinit pm8018_probe(struct platform_device *pdev)
536{
537 const struct pm8018_platform_data *pdata = pdev->dev.platform_data;
538 const char *revision_name = "unknown";
539 struct pm8018 *pmic;
540 enum pm8xxx_version version;
541 int revision;
542 int rc;
543 u8 val;
544
545 if (!pdata) {
546 pr_err("missing platform data\n");
547 return -EINVAL;
548 }
549
550 pmic = kzalloc(sizeof(struct pm8018), GFP_KERNEL);
551 if (!pmic) {
552 pr_err("Cannot alloc pm8018 struct\n");
553 return -ENOMEM;
554 }
555
556 /* Read PMIC chip revision */
557 rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV, &val, sizeof(val));
558 if (rc) {
559 pr_err("Failed to read hw rev 1 reg %d:rc=%d\n", REG_HWREV, rc);
560 goto err_read_rev;
561 }
562 pr_info("PMIC revision 1: %02X\n", val);
563 pmic->rev_registers = val;
564
565 /* Read PMIC chip revision 2 */
566 rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV_2, &val, sizeof(val));
567 if (rc) {
568 pr_err("Failed to read hw rev 2 reg %d:rc=%d\n", REG_HWREV_2,
569 rc);
570 goto err_read_rev;
571 }
572 pr_info("PMIC revision 2: %02X\n", val);
573 pmic->rev_registers |= val << BITS_PER_BYTE;
574
575 pmic->dev = &pdev->dev;
576 pm8018_drvdata.pm_chip_data = pmic;
577 platform_set_drvdata(pdev, &pm8018_drvdata);
578
579 /* Print out human readable version and revision names. */
580 version = pm8xxx_get_version(pmic->dev);
581 if (version == PM8XXX_VERSION_8018) {
582 revision = pm8xxx_get_revision(pmic->dev);
583 if (revision >= 0 && revision < ARRAY_SIZE(pm8018_rev_names))
584 revision_name = pm8018_rev_names[revision];
585 pr_info("PMIC version: PM8018 rev %s\n", revision_name);
586 } else {
587 WARN_ON(version != PM8XXX_VERSION_8018);
588 }
Jay Chokshib3c30e82011-10-17 19:11:35 -0700589 /* Log human readable restart reason */
590 rc = msm_ssbi_read(pdev->dev.parent, REG_PM8018_PON_CNTRL_3, &val, 1);
591 if (rc) {
592 pr_err("Cannot read restart reason rc=%d\n", rc);
593 goto err_read_rev;
594 }
Abhijeet Dharmapurikara4ed55c2012-08-28 15:07:43 -0700595 val &= PM8XXX_RESTART_REASON_MASK;
596 pr_info("PMIC Restart Reason: %s\n", pm8xxx_restart_reason_str[val]);
597 pmic->restart_reason = val;
David Collinsfdefdf92011-09-08 14:18:59 -0700598
599 rc = pm8018_add_subdevices(pdata, pmic);
600 if (rc) {
601 pr_err("Cannot add subdevices rc=%d\n", rc);
602 goto err;
603 }
604
605 /* gpio might not work if no irq device is found */
606 WARN_ON(pmic->irq_chip == NULL);
607
608 return 0;
609
610err:
611 mfd_remove_devices(pmic->dev);
612 platform_set_drvdata(pdev, NULL);
David Collins8c695c52011-12-12 15:46:02 -0800613 kfree(pmic->mfd_regulators);
614 kfree(pmic->regulator_cdata);
David Collinsfdefdf92011-09-08 14:18:59 -0700615err_read_rev:
616 kfree(pmic);
617 return rc;
618}
619
620static int __devexit pm8018_remove(struct platform_device *pdev)
621{
622 struct pm8xxx_drvdata *drvdata;
623 struct pm8018 *pmic = NULL;
David Collins8c695c52011-12-12 15:46:02 -0800624 int i;
David Collinsfdefdf92011-09-08 14:18:59 -0700625
626 drvdata = platform_get_drvdata(pdev);
627 if (drvdata)
628 pmic = drvdata->pm_chip_data;
David Collins8c695c52011-12-12 15:46:02 -0800629 if (pmic) {
630 if (pmic->dev)
631 mfd_remove_devices(pmic->dev);
632 if (pmic->irq_chip) {
633 pm8xxx_irq_exit(pmic->irq_chip);
634 pmic->irq_chip = NULL;
635 }
636 if (pmic->mfd_regulators) {
637 for (i = 0; i < ARRAY_SIZE(regulator_data); i++)
638 mutex_destroy(&regulator_data[i].pc_lock);
639 }
640 kfree(pmic->mfd_regulators);
641 kfree(pmic->regulator_cdata);
642 kfree(pmic);
David Collinsfdefdf92011-09-08 14:18:59 -0700643 }
644 platform_set_drvdata(pdev, NULL);
David Collinsfdefdf92011-09-08 14:18:59 -0700645
646 return 0;
647}
648
649static struct platform_driver pm8018_driver = {
650 .probe = pm8018_probe,
651 .remove = __devexit_p(pm8018_remove),
652 .driver = {
653 .name = PM8018_CORE_DEV_NAME,
654 .owner = THIS_MODULE,
655 },
656};
657
658static int __init pm8018_init(void)
659{
660 return platform_driver_register(&pm8018_driver);
661}
662postcore_initcall(pm8018_init);
663
664static void __exit pm8018_exit(void)
665{
666 platform_driver_unregister(&pm8018_driver);
667}
668module_exit(pm8018_exit);
669
670MODULE_LICENSE("GPL v2");
671MODULE_DESCRIPTION("PMIC 8018 core driver");
672MODULE_VERSION("1.0");
673MODULE_ALIAS("platform:" PM8018_CORE_DEV_NAME);