David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 1 | /* |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 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> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
David Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 20 | #include <linux/string.h> |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 21 | #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 Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 26 | #include <linux/mfd/pm8xxx/regulator.h> |
Jay Chokshi | e91f55c | 2011-10-17 15:10:07 -0700 | [diff] [blame] | 27 | #include <linux/leds-pm8xxx.h> |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 28 | |
| 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 Ghayal | ca42c7de | 2011-11-21 10:42:07 +0530 | [diff] [blame] | 35 | #define REG_IRQ_BASE 0x1BB |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 36 | |
| 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 Chokshi | b3c30e8 | 2011-10-17 19:11:35 -0700 | [diff] [blame] | 47 | #define REG_PM8018_PON_CNTRL_3 0x01D |
Jay Chokshi | b3c30e8 | 2011-10-17 19:11:35 -0700 | [diff] [blame] | 48 | |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 49 | #define SINGLE_IRQ_RESOURCE(_name, _irq) \ |
| 50 | { \ |
| 51 | .name = _name, \ |
| 52 | .start = _irq, \ |
| 53 | .end = _irq, \ |
| 54 | .flags = IORESOURCE_IRQ, \ |
| 55 | } |
| 56 | |
| 57 | struct pm8018 { |
David Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 58 | 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 Dharmapurikar | a4ed55c | 2012-08-28 15:07:43 -0700 | [diff] [blame] | 63 | u8 restart_reason; |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static 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 | |
| 74 | static 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 | |
| 82 | static 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 | |
| 91 | static 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 | |
| 100 | static 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 | |
| 108 | static 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 | |
| 120 | static 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 Dharmapurikar | a4ed55c | 2012-08-28 15:07:43 -0700 | [diff] [blame] | 128 | static 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 Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 136 | static 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 Dharmapurikar | a4ed55c | 2012-08-28 15:07:43 -0700 | [diff] [blame] | 144 | .pmic_restart_reason = pm8018_restart_reason, |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | static 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 | |
| 156 | static 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 Mohanadoss | 5bcd206 | 2011-10-06 15:51:41 -0700 | [diff] [blame] | 163 | static 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 | |
| 169 | static 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 Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 176 | static 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 | |
| 185 | static 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 | |
| 192 | static 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 | |
| 202 | static 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 | |
| 209 | static 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 | |
| 214 | static 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 | |
| 221 | static struct mfd_cell misc_cell __devinitdata = { |
| 222 | .name = PM8XXX_MISC_DEV_NAME, |
| 223 | .id = -1, |
| 224 | }; |
| 225 | |
| 226 | static 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 Chokshi | e91f55c | 2011-10-17 15:10:07 -0700 | [diff] [blame] | 233 | static struct mfd_cell pwm_cell __devinitdata = { |
| 234 | .name = PM8XXX_PWM_DEV_NAME, |
| 235 | .id = -1, |
| 236 | }; |
| 237 | |
| 238 | static struct mfd_cell leds_cell __devinitdata = { |
| 239 | .name = PM8XXX_LEDS_DEV_NAME, |
| 240 | .id = -1, |
| 241 | }; |
| 242 | |
David Collins | 39914be2 | 2012-03-20 09:47:21 -0700 | [diff] [blame] | 243 | static 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 | |
| 248 | static 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 | |
| 258 | static 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 Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 267 | static 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 Collins | 1b570d2 | 2012-01-30 15:40:55 -0800 | [diff] [blame] | 290 | /* name pc_name ctrl test */ |
| 291 | VS("8018_lvs1", "8018_lvs1_pc", 0x060, 0x061), |
David Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | #define MAX_NAME_COMPARISON_LEN 32 |
| 295 | |
| 296 | static int __devinit match_regulator( |
Mark Brown | 5d028cd | 2011-10-14 13:36:04 +0100 | [diff] [blame] | 297 | struct pm8xxx_regulator_core_platform_data *core_data, const char *name) |
David Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 298 | { |
| 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 = ®ulator_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 = ®ulator_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 | |
| 326 | static int __devinit |
| 327 | pm8018_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(®ulator_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 | |
| 383 | bail: |
| 384 | for (i = 0; i < ARRAY_SIZE(regulator_data); i++) |
| 385 | mutex_destroy(®ulator_data[i].pc_lock); |
| 386 | kfree(mfd_regulators); |
| 387 | kfree(cdata); |
| 388 | return ret; |
| 389 | } |
| 390 | |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 391 | static int __devinit |
| 392 | pm8018_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 Ghayal | ca42c7de | 2011-11-21 10:42:07 +0530 | [diff] [blame] | 400 | pdata->irq_pdata->irq_cdata.base_addr = REG_IRQ_BASE; |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 401 | 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 Mohanadoss | 5bcd206 | 2011-10-06 15:51:41 -0700 | [diff] [blame] | 471 | 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 Chokshi | e91f55c | 2011-10-17 15:10:07 -0700 | [diff] [blame] | 478 | } |
| 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 Mohanadoss | 5bcd206 | 2011-10-06 15:51:41 -0700 | [diff] [blame] | 487 | goto bail; |
| 488 | } |
| 489 | } |
| 490 | |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 491 | 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 Chokshi | e91f55c | 2011-10-17 15:10:07 -0700 | [diff] [blame] | 497 | 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 Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 503 | if (pdata->num_regulators > 0 && pdata->regulator_pdatas) { |
David Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 504 | ret = pm8018_add_regulators(pdata, pmic, irq_base); |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 505 | if (ret) { |
| 506 | pr_err("Failed to add regulator subdevices ret=%d\n", |
| 507 | ret); |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 508 | goto bail; |
| 509 | } |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 510 | } |
| 511 | |
David Collins | 39914be2 | 2012-03-20 09:47:21 -0700 | [diff] [blame] | 512 | 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 Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 518 | |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 519 | return 0; |
| 520 | bail: |
| 521 | if (pmic->irq_chip) { |
| 522 | pm8xxx_irq_exit(pmic->irq_chip); |
| 523 | pmic->irq_chip = NULL; |
| 524 | } |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | static const char * const pm8018_rev_names[] = { |
| 529 | [PM8XXX_REVISION_8018_TEST] = "test", |
| 530 | [PM8XXX_REVISION_8018_1p0] = "1.0", |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 531 | [PM8XXX_REVISION_8018_2p0] = "2.0", |
Jay Chokshi | 1ba115d | 2012-04-19 15:42:41 -0700 | [diff] [blame] | 532 | [PM8XXX_REVISION_8018_2p1] = "2.1", |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | static 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 Chokshi | b3c30e8 | 2011-10-17 19:11:35 -0700 | [diff] [blame] | 589 | /* 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 Dharmapurikar | a4ed55c | 2012-08-28 15:07:43 -0700 | [diff] [blame] | 595 | val &= PM8XXX_RESTART_REASON_MASK; |
| 596 | pr_info("PMIC Restart Reason: %s\n", pm8xxx_restart_reason_str[val]); |
| 597 | pmic->restart_reason = val; |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 598 | |
| 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 | |
| 610 | err: |
| 611 | mfd_remove_devices(pmic->dev); |
| 612 | platform_set_drvdata(pdev, NULL); |
David Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 613 | kfree(pmic->mfd_regulators); |
| 614 | kfree(pmic->regulator_cdata); |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 615 | err_read_rev: |
| 616 | kfree(pmic); |
| 617 | return rc; |
| 618 | } |
| 619 | |
| 620 | static int __devexit pm8018_remove(struct platform_device *pdev) |
| 621 | { |
| 622 | struct pm8xxx_drvdata *drvdata; |
| 623 | struct pm8018 *pmic = NULL; |
David Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 624 | int i; |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 625 | |
| 626 | drvdata = platform_get_drvdata(pdev); |
| 627 | if (drvdata) |
| 628 | pmic = drvdata->pm_chip_data; |
David Collins | 8c695c5 | 2011-12-12 15:46:02 -0800 | [diff] [blame] | 629 | 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(®ulator_data[i].pc_lock); |
| 639 | } |
| 640 | kfree(pmic->mfd_regulators); |
| 641 | kfree(pmic->regulator_cdata); |
| 642 | kfree(pmic); |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 643 | } |
| 644 | platform_set_drvdata(pdev, NULL); |
David Collins | fdefdf9 | 2011-09-08 14:18:59 -0700 | [diff] [blame] | 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static 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 | |
| 658 | static int __init pm8018_init(void) |
| 659 | { |
| 660 | return platform_driver_register(&pm8018_driver); |
| 661 | } |
| 662 | postcore_initcall(pm8018_init); |
| 663 | |
| 664 | static void __exit pm8018_exit(void) |
| 665 | { |
| 666 | platform_driver_unregister(&pm8018_driver); |
| 667 | } |
| 668 | module_exit(pm8018_exit); |
| 669 | |
| 670 | MODULE_LICENSE("GPL v2"); |
| 671 | MODULE_DESCRIPTION("PMIC 8018 core driver"); |
| 672 | MODULE_VERSION("1.0"); |
| 673 | MODULE_ALIAS("platform:" PM8018_CORE_DEV_NAME); |