Merge git://git.kernel.org/pub/scm/linux/kernel/git/lwfinger/r8192E into staging...
[zen-stable.git] / drivers / mfd / pm8921-core.c
blobe873b15753d86aa356e6357035ee4a2b9a5df865
1 /*
2 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
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.
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.
14 #define pr_fmt(fmt) "%s: " fmt, __func__
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/msm_ssbi.h>
21 #include <linux/mfd/core.h>
22 #include <linux/mfd/pm8xxx/pm8921.h>
23 #include <linux/mfd/pm8xxx/core.h>
25 #define REG_HWREV 0x002 /* PMIC4 revision */
26 #define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */
28 struct pm8921 {
29 struct device *dev;
30 struct pm_irq_chip *irq_chip;
33 static int pm8921_readb(const struct device *dev, u16 addr, u8 *val)
35 const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
36 const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
38 return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
41 static int pm8921_writeb(const struct device *dev, u16 addr, u8 val)
43 const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
44 const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
46 return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
49 static int pm8921_read_buf(const struct device *dev, u16 addr, u8 *buf,
50 int cnt)
52 const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
53 const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
55 return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
58 static int pm8921_write_buf(const struct device *dev, u16 addr, u8 *buf,
59 int cnt)
61 const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
62 const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
64 return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
67 static int pm8921_read_irq_stat(const struct device *dev, int irq)
69 const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
70 const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
72 return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
75 static struct pm8xxx_drvdata pm8921_drvdata = {
76 .pmic_readb = pm8921_readb,
77 .pmic_writeb = pm8921_writeb,
78 .pmic_read_buf = pm8921_read_buf,
79 .pmic_write_buf = pm8921_write_buf,
80 .pmic_read_irq_stat = pm8921_read_irq_stat,
83 static int __devinit pm8921_add_subdevices(const struct pm8921_platform_data
84 *pdata,
85 struct pm8921 *pmic,
86 u32 rev)
88 int ret = 0, irq_base = 0;
89 struct pm_irq_chip *irq_chip;
91 if (pdata->irq_pdata) {
92 pdata->irq_pdata->irq_cdata.nirqs = PM8921_NR_IRQS;
93 pdata->irq_pdata->irq_cdata.rev = rev;
94 irq_base = pdata->irq_pdata->irq_base;
95 irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
97 if (IS_ERR(irq_chip)) {
98 pr_err("Failed to init interrupts ret=%ld\n",
99 PTR_ERR(irq_chip));
100 return PTR_ERR(irq_chip);
102 pmic->irq_chip = irq_chip;
104 return ret;
107 static int __devinit pm8921_probe(struct platform_device *pdev)
109 const struct pm8921_platform_data *pdata = pdev->dev.platform_data;
110 struct pm8921 *pmic;
111 int rc;
112 u8 val;
113 u32 rev;
115 if (!pdata) {
116 pr_err("missing platform data\n");
117 return -EINVAL;
120 pmic = kzalloc(sizeof(struct pm8921), GFP_KERNEL);
121 if (!pmic) {
122 pr_err("Cannot alloc pm8921 struct\n");
123 return -ENOMEM;
126 /* Read PMIC chip revision */
127 rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV, &val, sizeof(val));
128 if (rc) {
129 pr_err("Failed to read hw rev reg %d:rc=%d\n", REG_HWREV, rc);
130 goto err_read_rev;
132 pr_info("PMIC revision 1: %02X\n", val);
133 rev = val;
135 /* Read PMIC chip revision 2 */
136 rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV_2, &val, sizeof(val));
137 if (rc) {
138 pr_err("Failed to read hw rev 2 reg %d:rc=%d\n",
139 REG_HWREV_2, rc);
140 goto err_read_rev;
142 pr_info("PMIC revision 2: %02X\n", val);
143 rev |= val << BITS_PER_BYTE;
145 pmic->dev = &pdev->dev;
146 pm8921_drvdata.pm_chip_data = pmic;
147 platform_set_drvdata(pdev, &pm8921_drvdata);
149 rc = pm8921_add_subdevices(pdata, pmic, rev);
150 if (rc) {
151 pr_err("Cannot add subdevices rc=%d\n", rc);
152 goto err;
155 /* gpio might not work if no irq device is found */
156 WARN_ON(pmic->irq_chip == NULL);
158 return 0;
160 err:
161 mfd_remove_devices(pmic->dev);
162 platform_set_drvdata(pdev, NULL);
163 err_read_rev:
164 kfree(pmic);
165 return rc;
168 static int __devexit pm8921_remove(struct platform_device *pdev)
170 struct pm8xxx_drvdata *drvdata;
171 struct pm8921 *pmic = NULL;
173 drvdata = platform_get_drvdata(pdev);
174 if (drvdata)
175 pmic = drvdata->pm_chip_data;
176 if (pmic)
177 mfd_remove_devices(pmic->dev);
178 if (pmic->irq_chip) {
179 pm8xxx_irq_exit(pmic->irq_chip);
180 pmic->irq_chip = NULL;
182 platform_set_drvdata(pdev, NULL);
183 kfree(pmic);
185 return 0;
188 static struct platform_driver pm8921_driver = {
189 .probe = pm8921_probe,
190 .remove = __devexit_p(pm8921_remove),
191 .driver = {
192 .name = "pm8921-core",
193 .owner = THIS_MODULE,
197 static int __init pm8921_init(void)
199 return platform_driver_register(&pm8921_driver);
201 subsys_initcall(pm8921_init);
203 static void __exit pm8921_exit(void)
205 platform_driver_unregister(&pm8921_driver);
207 module_exit(pm8921_exit);
209 MODULE_LICENSE("GPL v2");
210 MODULE_DESCRIPTION("PMIC 8921 core driver");
211 MODULE_VERSION("1.0");
212 MODULE_ALIAS("platform:pm8921-core");