blob: aec382a55e7358ce22589a5fd973cb2cbfb3b3a0 [file] [log] [blame]
Stephen Boyd9a8f4eb2012-02-21 23:51:00 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/interrupt.h>
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053014#include <linux/platform_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070016#include <linux/mfd/core.h>
Anirudh Ghayalc8051a82011-11-17 09:28:24 +053017#include <linux/msm_ssbi.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070018#include <linux/mfd/pmic8901.h>
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053019#include <linux/mfd/pm8xxx/core.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020
21/* PMIC8901 Revision */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053022#define PM8901_REG_REV 0x002
23#define PM8901_VERSION_MASK 0xF0
24#define PM8901_REVISION_MASK 0x0F
25#define PM8901_VERSION_VALUE 0xF0
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053027#define REG_IRQ_BASE 0xD5
28#define REG_MPP_BASE 0x27
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053030#define REG_TEMP_ALRM_CTRL 0x23
31#define REG_TEMP_ALRM_PWM 0x24
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053033#define SINGLE_IRQ_RESOURCE(_name, _irq) \
34{ \
35 .name = _name, \
36 .start = _irq, \
37 .end = _irq, \
38 .flags = IORESOURCE_IRQ, \
39}
40
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041struct pm8901_chip {
42 struct pm8901_platform_data pdata;
Anirudh Ghayalc8051a82011-11-17 09:28:24 +053043 struct device *dev;
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053044 struct pm_irq_chip *irq_chip;
45 struct mfd_cell *mfd_regulators;
46 u8 revision;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047};
48
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053049static int pm8901_readb(const struct device *dev, u16 addr, u8 *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053051 const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
52 const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053054 return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055}
56
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053057static int pm8901_writeb(const struct device *dev, u16 addr, u8 val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053059 const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
60 const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053062 return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063}
64
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053065static int pm8901_read_buf(const struct device *dev, u16 addr, u8 *buf,
66 int cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053068 const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
69 const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053071 return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072}
73
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053074static int pm8901_write_buf(const struct device *dev, u16 addr, u8 *buf,
75 int cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053077 const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
78 const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053080 return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081}
82
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053083static int pm8901_read_irq_stat(const struct device *dev, int irq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053085 const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
86 const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053088 return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089
90 return 0;
91}
92
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053093static enum pm8xxx_version pm8901_get_version(const struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053095 const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
96 const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
97 enum pm8xxx_version version = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053099 if ((pmic->revision & PM8901_VERSION_MASK) == PM8901_VERSION_VALUE)
100 version = PM8XXX_VERSION_8901;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530102 return version;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103}
104
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530105static int pm8901_get_revision(const struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530107 const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
108 const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530110 return pmic->revision & PM8901_REVISION_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111}
112
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530113static struct pm8xxx_drvdata pm8901_drvdata = {
114 .pmic_readb = pm8901_readb,
115 .pmic_writeb = pm8901_writeb,
116 .pmic_read_buf = pm8901_read_buf,
117 .pmic_write_buf = pm8901_write_buf,
118 .pmic_read_irq_stat = pm8901_read_irq_stat,
119 .pmic_get_version = pm8901_get_version,
120 .pmic_get_revision = pm8901_get_revision,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121};
122
Anirudh Ghayal934b2712011-12-13 12:49:51 +0530123static struct mfd_cell misc_cell = {
124 .name = PM8XXX_MISC_DEV_NAME,
125 .id = 1,
126};
127
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530128static struct mfd_cell debugfs_cell = {
129 .name = "pm8xxx-debug",
130 .id = 1,
131 .platform_data = "pm8901-dbg",
132 .pdata_size = sizeof("pm8901-dbg"),
133};
134
135static const struct resource thermal_alarm_cell_resources[] = {
136 SINGLE_IRQ_RESOURCE("pm8901_tempstat_irq", PM8901_TEMPSTAT_IRQ),
137 SINGLE_IRQ_RESOURCE("pm8901_overtemp_irq", PM8901_OVERTEMP_IRQ),
138};
139
140static struct pm8xxx_tm_core_data thermal_alarm_cdata = {
141 .adc_type = PM8XXX_TM_ADC_NONE,
142 .reg_addr_temp_alarm_ctrl = REG_TEMP_ALRM_CTRL,
143 .reg_addr_temp_alarm_pwm = REG_TEMP_ALRM_PWM,
144 .tm_name = "pm8901_tz",
145 .irq_name_temp_stat = "pm8901_tempstat_irq",
146 .irq_name_over_temp = "pm8901_overtemp_irq",
147};
148
149static struct mfd_cell thermal_alarm_cell = {
150 .name = PM8XXX_TM_DEV_NAME,
151 .id = 1,
152 .resources = thermal_alarm_cell_resources,
153 .num_resources = ARRAY_SIZE(thermal_alarm_cell_resources),
154 .platform_data = &thermal_alarm_cdata,
155 .pdata_size = sizeof(struct pm8xxx_tm_core_data),
156};
157
158static const struct resource mpp_cell_resources[] = {
159 {
160 .start = PM8901_IRQ_BLOCK_BIT(PM8901_MPP_BLOCK_START, 0),
161 .end = PM8901_IRQ_BLOCK_BIT(PM8901_MPP_BLOCK_START, 0)
162 + PM8901_MPPS - 1,
163 .flags = IORESOURCE_IRQ,
164 },
165};
166
167static struct mfd_cell mpp_cell = {
168 .name = PM8XXX_MPP_DEV_NAME,
169 .id = 1,
170 .resources = mpp_cell_resources,
171 .num_resources = ARRAY_SIZE(mpp_cell_resources),
172};
173
174static int __devinit
175pm8901_add_subdevices(const struct pm8901_platform_data *pdata,
176 struct pm8901_chip *pmic)
177{
178 int rc = 0, irq_base = 0, i;
179 struct pm_irq_chip *irq_chip;
180 static struct mfd_cell *mfd_regulators;
181
182 if (pdata->irq_pdata) {
183 pdata->irq_pdata->irq_cdata.nirqs = PM8901_NR_IRQS;
184 pdata->irq_pdata->irq_cdata.base_addr = REG_IRQ_BASE;
185 irq_base = pdata->irq_pdata->irq_base;
186 irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
187
188 if (IS_ERR(irq_chip)) {
189 pr_err("Failed to init interrupts ret=%ld\n",
190 PTR_ERR(irq_chip));
191 return PTR_ERR(irq_chip);
192 }
193 pmic->irq_chip = irq_chip;
194 }
195
196 if (pdata->mpp_pdata) {
197 pdata->mpp_pdata->core_data.nmpps = PM8901_MPPS;
198 pdata->mpp_pdata->core_data.base_addr = REG_MPP_BASE;
199 mpp_cell.platform_data = pdata->mpp_pdata;
200 mpp_cell.pdata_size = sizeof(struct pm8xxx_mpp_platform_data);
201 rc = mfd_add_devices(pmic->dev, 0, &mpp_cell, 1, NULL,
202 irq_base);
203 if (rc) {
204 pr_err("Failed to add mpp subdevice ret=%d\n", rc);
205 goto bail;
206 }
207 }
208
209 if (pdata->num_regulators > 0 && pdata->regulator_pdatas) {
210 mfd_regulators = kzalloc(sizeof(struct mfd_cell)
211 * (pdata->num_regulators), GFP_KERNEL);
212 if (!mfd_regulators) {
213 pr_err("Cannot allocate %d bytes for pm8901 regulator "
214 "mfd cells\n", sizeof(struct mfd_cell)
215 * (pdata->num_regulators));
216 rc = -ENOMEM;
217 goto bail;
218 }
219 for (i = 0; i < pdata->num_regulators; i++) {
220 mfd_regulators[i].name = "pm8901-regulator";
221 mfd_regulators[i].id = pdata->regulator_pdatas[i].id;
222 mfd_regulators[i].platform_data =
223 &(pdata->regulator_pdatas[i]);
224 mfd_regulators[i].pdata_size =
225 sizeof(struct pm8901_vreg_pdata);
226 }
227 rc = mfd_add_devices(pmic->dev, 0, mfd_regulators,
228 pdata->num_regulators, NULL, irq_base);
229 if (rc) {
230 pr_err("Failed to add regulator subdevices ret=%d\n",
231 rc);
232 kfree(mfd_regulators);
233 goto bail;
234 }
235 pmic->mfd_regulators = mfd_regulators;
236 }
237
238 rc = mfd_add_devices(pmic->dev, 0, &thermal_alarm_cell, 1, NULL,
239 irq_base);
240 if (rc) {
241 pr_err("Failed to add thermal alarm subdevice ret=%d\n",
242 rc);
243 goto bail;
244 }
245
246 rc = mfd_add_devices(pmic->dev, 0, &debugfs_cell, 1, NULL, irq_base);
247 if (rc) {
248 pr_err("Failed to add debugfs subdevice ret=%d\n", rc);
249 goto bail;
250 }
251
Anirudh Ghayal934b2712011-12-13 12:49:51 +0530252 if (pdata->misc_pdata) {
253 misc_cell.platform_data = pdata->misc_pdata;
254 misc_cell.pdata_size = sizeof(struct pm8xxx_misc_platform_data);
255 rc = mfd_add_devices(pmic->dev, 0, &misc_cell, 1, NULL,
256 irq_base);
257 if (rc) {
258 pr_err("Failed to add misc subdevice ret=%d\n", rc);
259 goto bail;
260 }
261 }
262
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530263 return rc;
264
265bail:
266 if (pmic->irq_chip) {
267 pm8xxx_irq_exit(pmic->irq_chip);
268 pmic->irq_chip = NULL;
269 }
270 return rc;
271}
272
Stephen Boyd9a8f4eb2012-02-21 23:51:00 -0800273static int __devinit pm8901_probe(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530275 int rc;
Anirudh Ghayalc8051a82011-11-17 09:28:24 +0530276 struct pm8901_platform_data *pdata = pdev->dev.platform_data;
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530277 struct pm8901_chip *pmic;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530279 if (pdata == NULL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 pr_err("%s: No platform_data or IRQ.\n", __func__);
281 return -ENODEV;
282 }
283
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530284 pmic = kzalloc(sizeof *pmic, GFP_KERNEL);
285 if (pmic == NULL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 pr_err("%s: kzalloc() failed.\n", __func__);
287 return -ENOMEM;
288 }
289
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530290 pmic->dev = &pdev->dev;
291
292 pm8901_drvdata.pm_chip_data = pmic;
293 platform_set_drvdata(pdev, &pm8901_drvdata);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294
295 /* Read PMIC chip revision */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530296 rc = pm8901_readb(pmic->dev, PM8901_REG_REV, &pmic->revision);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 if (rc)
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530298 pr_err("%s: Failed reading version register rc=%d.\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 __func__, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530301 pr_info("%s: PMIC REVISION = %X\n", __func__, pmic->revision);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530303 (void) memcpy((void *)&pmic->pdata, (const void *)pdata,
304 sizeof(pmic->pdata));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530306 rc = pm8901_add_subdevices(pdata, pmic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 if (rc) {
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530308 pr_err("Cannot add subdevices rc=%d\n", rc);
309 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 }
311
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530312 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530314err:
315 platform_set_drvdata(pdev, NULL);
316 kfree(pmic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 return rc;
318}
319
Anirudh Ghayalc8051a82011-11-17 09:28:24 +0530320static int __devexit pm8901_remove(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321{
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530322 struct pm8xxx_drvdata *drvdata;
323 struct pm8901_chip *pmic = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530325 drvdata = platform_get_drvdata(pdev);
326 if (drvdata)
327 pmic = drvdata->pm_chip_data;
328 if (pmic) {
329 if (pmic->dev)
330 mfd_remove_devices(pmic->dev);
331 if (pmic->irq_chip)
332 pm8xxx_irq_exit(pmic->irq_chip);
333 kfree(pmic->mfd_regulators);
334 kfree(pmic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 }
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530336 platform_set_drvdata(pdev, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337
338 return 0;
339}
340
Anirudh Ghayalc8051a82011-11-17 09:28:24 +0530341static struct platform_driver pm8901_driver = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 .probe = pm8901_probe,
343 .remove = __devexit_p(pm8901_remove),
Anirudh Ghayalc8051a82011-11-17 09:28:24 +0530344 .driver = {
345 .name = "pm8901-core",
346 .owner = THIS_MODULE,
347 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348};
349
350static int __init pm8901_init(void)
351{
Anirudh Ghayalc8051a82011-11-17 09:28:24 +0530352 return platform_driver_register(&pm8901_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353}
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530354postcore_initcall(pm8901_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355
356static void __exit pm8901_exit(void)
357{
Anirudh Ghayalc8051a82011-11-17 09:28:24 +0530358 platform_driver_unregister(&pm8901_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360module_exit(pm8901_exit);
361
362MODULE_LICENSE("GPL v2");
363MODULE_DESCRIPTION("PMIC8901 core driver");
364MODULE_VERSION("1.0");
365MODULE_ALIAS("platform:pmic8901-core");