Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Jingoo Han | ef310f4 | 2013-08-20 16:01:26 +0900 | [diff] [blame^] | 17 | #include <linux/module.h> |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 20 | #include <linux/err.h> |
David Brown | ce44bf5 | 2013-03-12 11:41:54 -0700 | [diff] [blame] | 21 | #include <linux/ssbi.h> |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 22 | #include <linux/mfd/core.h> |
| 23 | #include <linux/mfd/pm8xxx/pm8921.h> |
| 24 | #include <linux/mfd/pm8xxx/core.h> |
| 25 | |
| 26 | #define REG_HWREV 0x002 /* PMIC4 revision */ |
| 27 | #define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */ |
| 28 | |
| 29 | struct pm8921 { |
| 30 | struct device *dev; |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 31 | struct pm_irq_chip *irq_chip; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static int pm8921_readb(const struct device *dev, u16 addr, u8 *val) |
| 35 | { |
| 36 | const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev); |
| 37 | const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data; |
| 38 | |
David Brown | ce44bf5 | 2013-03-12 11:41:54 -0700 | [diff] [blame] | 39 | return ssbi_read(pmic->dev->parent, addr, val, 1); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static int pm8921_writeb(const struct device *dev, u16 addr, u8 val) |
| 43 | { |
| 44 | const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev); |
| 45 | const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data; |
| 46 | |
David Brown | ce44bf5 | 2013-03-12 11:41:54 -0700 | [diff] [blame] | 47 | return ssbi_write(pmic->dev->parent, addr, &val, 1); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static int pm8921_read_buf(const struct device *dev, u16 addr, u8 *buf, |
| 51 | int cnt) |
| 52 | { |
| 53 | const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev); |
| 54 | const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data; |
| 55 | |
David Brown | ce44bf5 | 2013-03-12 11:41:54 -0700 | [diff] [blame] | 56 | return ssbi_read(pmic->dev->parent, addr, buf, cnt); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static int pm8921_write_buf(const struct device *dev, u16 addr, u8 *buf, |
| 60 | int cnt) |
| 61 | { |
| 62 | const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev); |
| 63 | const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data; |
| 64 | |
David Brown | ce44bf5 | 2013-03-12 11:41:54 -0700 | [diff] [blame] | 65 | return ssbi_write(pmic->dev->parent, addr, buf, cnt); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 68 | static int pm8921_read_irq_stat(const struct device *dev, int irq) |
| 69 | { |
| 70 | const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev); |
| 71 | const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data; |
| 72 | |
| 73 | return pm8xxx_get_irq_stat(pmic->irq_chip, irq); |
| 74 | } |
| 75 | |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 76 | static struct pm8xxx_drvdata pm8921_drvdata = { |
| 77 | .pmic_readb = pm8921_readb, |
| 78 | .pmic_writeb = pm8921_writeb, |
| 79 | .pmic_read_buf = pm8921_read_buf, |
| 80 | .pmic_write_buf = pm8921_write_buf, |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 81 | .pmic_read_irq_stat = pm8921_read_irq_stat, |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 84 | static int pm8921_add_subdevices(const struct pm8921_platform_data |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 85 | *pdata, |
| 86 | struct pm8921 *pmic, |
| 87 | u32 rev) |
| 88 | { |
| 89 | int ret = 0, irq_base = 0; |
| 90 | struct pm_irq_chip *irq_chip; |
| 91 | |
| 92 | if (pdata->irq_pdata) { |
| 93 | pdata->irq_pdata->irq_cdata.nirqs = PM8921_NR_IRQS; |
| 94 | pdata->irq_pdata->irq_cdata.rev = rev; |
| 95 | irq_base = pdata->irq_pdata->irq_base; |
| 96 | irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata); |
| 97 | |
| 98 | if (IS_ERR(irq_chip)) { |
| 99 | pr_err("Failed to init interrupts ret=%ld\n", |
| 100 | PTR_ERR(irq_chip)); |
| 101 | return PTR_ERR(irq_chip); |
| 102 | } |
| 103 | pmic->irq_chip = irq_chip; |
| 104 | } |
| 105 | return ret; |
| 106 | } |
| 107 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 108 | static int pm8921_probe(struct platform_device *pdev) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 109 | { |
Jingoo Han | 334a41c | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 110 | const struct pm8921_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 111 | struct pm8921 *pmic; |
| 112 | int rc; |
| 113 | u8 val; |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 114 | u32 rev; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 115 | |
| 116 | if (!pdata) { |
| 117 | pr_err("missing platform data\n"); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | pmic = kzalloc(sizeof(struct pm8921), GFP_KERNEL); |
| 122 | if (!pmic) { |
| 123 | pr_err("Cannot alloc pm8921 struct\n"); |
| 124 | return -ENOMEM; |
| 125 | } |
| 126 | |
| 127 | /* Read PMIC chip revision */ |
David Brown | ce44bf5 | 2013-03-12 11:41:54 -0700 | [diff] [blame] | 128 | rc = ssbi_read(pdev->dev.parent, REG_HWREV, &val, sizeof(val)); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 129 | if (rc) { |
| 130 | pr_err("Failed to read hw rev reg %d:rc=%d\n", REG_HWREV, rc); |
| 131 | goto err_read_rev; |
| 132 | } |
| 133 | pr_info("PMIC revision 1: %02X\n", val); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 134 | rev = val; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 135 | |
| 136 | /* Read PMIC chip revision 2 */ |
David Brown | ce44bf5 | 2013-03-12 11:41:54 -0700 | [diff] [blame] | 137 | rc = ssbi_read(pdev->dev.parent, REG_HWREV_2, &val, sizeof(val)); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 138 | if (rc) { |
| 139 | pr_err("Failed to read hw rev 2 reg %d:rc=%d\n", |
| 140 | REG_HWREV_2, rc); |
| 141 | goto err_read_rev; |
| 142 | } |
| 143 | pr_info("PMIC revision 2: %02X\n", val); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 144 | rev |= val << BITS_PER_BYTE; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 145 | |
| 146 | pmic->dev = &pdev->dev; |
| 147 | pm8921_drvdata.pm_chip_data = pmic; |
| 148 | platform_set_drvdata(pdev, &pm8921_drvdata); |
| 149 | |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 150 | rc = pm8921_add_subdevices(pdata, pmic, rev); |
| 151 | if (rc) { |
| 152 | pr_err("Cannot add subdevices rc=%d\n", rc); |
| 153 | goto err; |
| 154 | } |
| 155 | |
| 156 | /* gpio might not work if no irq device is found */ |
| 157 | WARN_ON(pmic->irq_chip == NULL); |
| 158 | |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 161 | err: |
| 162 | mfd_remove_devices(pmic->dev); |
| 163 | platform_set_drvdata(pdev, NULL); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 164 | err_read_rev: |
| 165 | kfree(pmic); |
| 166 | return rc; |
| 167 | } |
| 168 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 169 | static int pm8921_remove(struct platform_device *pdev) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 170 | { |
| 171 | struct pm8xxx_drvdata *drvdata; |
| 172 | struct pm8921 *pmic = NULL; |
| 173 | |
| 174 | drvdata = platform_get_drvdata(pdev); |
| 175 | if (drvdata) |
| 176 | pmic = drvdata->pm_chip_data; |
| 177 | if (pmic) |
| 178 | mfd_remove_devices(pmic->dev); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 179 | if (pmic->irq_chip) { |
| 180 | pm8xxx_irq_exit(pmic->irq_chip); |
| 181 | pmic->irq_chip = NULL; |
| 182 | } |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 183 | platform_set_drvdata(pdev, NULL); |
| 184 | kfree(pmic); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static struct platform_driver pm8921_driver = { |
| 190 | .probe = pm8921_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 191 | .remove = pm8921_remove, |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 192 | .driver = { |
| 193 | .name = "pm8921-core", |
| 194 | .owner = THIS_MODULE, |
| 195 | }, |
| 196 | }; |
| 197 | |
| 198 | static int __init pm8921_init(void) |
| 199 | { |
| 200 | return platform_driver_register(&pm8921_driver); |
| 201 | } |
| 202 | subsys_initcall(pm8921_init); |
| 203 | |
| 204 | static void __exit pm8921_exit(void) |
| 205 | { |
| 206 | platform_driver_unregister(&pm8921_driver); |
| 207 | } |
| 208 | module_exit(pm8921_exit); |
| 209 | |
| 210 | MODULE_LICENSE("GPL v2"); |
| 211 | MODULE_DESCRIPTION("PMIC 8921 core driver"); |
| 212 | MODULE_VERSION("1.0"); |
| 213 | MODULE_ALIAS("platform:pm8921-core"); |