Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Base driver for Marvell 88PM800 |
| 3 | * |
| 4 | * Copyright (C) 2012 Marvell International Ltd. |
| 5 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
| 6 | * Joseph(Yossi) Hanin <yhanin@marvell.com> |
| 7 | * Qiao Zhou <zhouqiao@marvell.com> |
| 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General |
| 10 | * Public License. See the file "COPYING" in the main directory of this |
| 11 | * archive for more details. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
Chao Xie | 5270534 | 2013-06-14 01:21:50 -0400 | [diff] [blame] | 25 | #include <linux/err.h> |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 26 | #include <linux/i2c.h> |
| 27 | #include <linux/mfd/core.h> |
| 28 | #include <linux/mfd/88pm80x.h> |
| 29 | #include <linux/slab.h> |
| 30 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 31 | /* Interrupt Registers */ |
| 32 | #define PM800_INT_STATUS1 (0x05) |
| 33 | #define PM800_ONKEY_INT_STS1 (1 << 0) |
| 34 | #define PM800_EXTON_INT_STS1 (1 << 1) |
| 35 | #define PM800_CHG_INT_STS1 (1 << 2) |
| 36 | #define PM800_BAT_INT_STS1 (1 << 3) |
| 37 | #define PM800_RTC_INT_STS1 (1 << 4) |
| 38 | #define PM800_CLASSD_OC_INT_STS1 (1 << 5) |
| 39 | |
| 40 | #define PM800_INT_STATUS2 (0x06) |
| 41 | #define PM800_VBAT_INT_STS2 (1 << 0) |
| 42 | #define PM800_VSYS_INT_STS2 (1 << 1) |
| 43 | #define PM800_VCHG_INT_STS2 (1 << 2) |
| 44 | #define PM800_TINT_INT_STS2 (1 << 3) |
| 45 | #define PM800_GPADC0_INT_STS2 (1 << 4) |
| 46 | #define PM800_TBAT_INT_STS2 (1 << 5) |
| 47 | #define PM800_GPADC2_INT_STS2 (1 << 6) |
| 48 | #define PM800_GPADC3_INT_STS2 (1 << 7) |
| 49 | |
| 50 | #define PM800_INT_STATUS3 (0x07) |
| 51 | |
| 52 | #define PM800_INT_STATUS4 (0x08) |
| 53 | #define PM800_GPIO0_INT_STS4 (1 << 0) |
| 54 | #define PM800_GPIO1_INT_STS4 (1 << 1) |
| 55 | #define PM800_GPIO2_INT_STS4 (1 << 2) |
| 56 | #define PM800_GPIO3_INT_STS4 (1 << 3) |
| 57 | #define PM800_GPIO4_INT_STS4 (1 << 4) |
| 58 | |
| 59 | #define PM800_INT_ENA_1 (0x09) |
| 60 | #define PM800_ONKEY_INT_ENA1 (1 << 0) |
| 61 | #define PM800_EXTON_INT_ENA1 (1 << 1) |
| 62 | #define PM800_CHG_INT_ENA1 (1 << 2) |
| 63 | #define PM800_BAT_INT_ENA1 (1 << 3) |
| 64 | #define PM800_RTC_INT_ENA1 (1 << 4) |
| 65 | #define PM800_CLASSD_OC_INT_ENA1 (1 << 5) |
| 66 | |
| 67 | #define PM800_INT_ENA_2 (0x0A) |
| 68 | #define PM800_VBAT_INT_ENA2 (1 << 0) |
| 69 | #define PM800_VSYS_INT_ENA2 (1 << 1) |
| 70 | #define PM800_VCHG_INT_ENA2 (1 << 2) |
| 71 | #define PM800_TINT_INT_ENA2 (1 << 3) |
| 72 | |
| 73 | #define PM800_INT_ENA_3 (0x0B) |
| 74 | #define PM800_GPADC0_INT_ENA3 (1 << 0) |
| 75 | #define PM800_GPADC1_INT_ENA3 (1 << 1) |
| 76 | #define PM800_GPADC2_INT_ENA3 (1 << 2) |
| 77 | #define PM800_GPADC3_INT_ENA3 (1 << 3) |
| 78 | #define PM800_GPADC4_INT_ENA3 (1 << 4) |
| 79 | |
| 80 | #define PM800_INT_ENA_4 (0x0C) |
| 81 | #define PM800_GPIO0_INT_ENA4 (1 << 0) |
| 82 | #define PM800_GPIO1_INT_ENA4 (1 << 1) |
| 83 | #define PM800_GPIO2_INT_ENA4 (1 << 2) |
| 84 | #define PM800_GPIO3_INT_ENA4 (1 << 3) |
| 85 | #define PM800_GPIO4_INT_ENA4 (1 << 4) |
| 86 | |
| 87 | /* number of INT_ENA & INT_STATUS regs */ |
| 88 | #define PM800_INT_REG_NUM (4) |
| 89 | |
| 90 | /* Interrupt Number in 88PM800 */ |
| 91 | enum { |
| 92 | PM800_IRQ_ONKEY, /*EN1b0 *//*0 */ |
| 93 | PM800_IRQ_EXTON, /*EN1b1 */ |
| 94 | PM800_IRQ_CHG, /*EN1b2 */ |
| 95 | PM800_IRQ_BAT, /*EN1b3 */ |
| 96 | PM800_IRQ_RTC, /*EN1b4 */ |
| 97 | PM800_IRQ_CLASSD, /*EN1b5 *//*5 */ |
| 98 | PM800_IRQ_VBAT, /*EN2b0 */ |
| 99 | PM800_IRQ_VSYS, /*EN2b1 */ |
| 100 | PM800_IRQ_VCHG, /*EN2b2 */ |
| 101 | PM800_IRQ_TINT, /*EN2b3 */ |
| 102 | PM800_IRQ_GPADC0, /*EN3b0 *//*10 */ |
| 103 | PM800_IRQ_GPADC1, /*EN3b1 */ |
| 104 | PM800_IRQ_GPADC2, /*EN3b2 */ |
| 105 | PM800_IRQ_GPADC3, /*EN3b3 */ |
| 106 | PM800_IRQ_GPADC4, /*EN3b4 */ |
| 107 | PM800_IRQ_GPIO0, /*EN4b0 *//*15 */ |
| 108 | PM800_IRQ_GPIO1, /*EN4b1 */ |
| 109 | PM800_IRQ_GPIO2, /*EN4b2 */ |
| 110 | PM800_IRQ_GPIO3, /*EN4b3 */ |
| 111 | PM800_IRQ_GPIO4, /*EN4b4 *//*19 */ |
| 112 | PM800_MAX_IRQ, |
| 113 | }; |
| 114 | |
Chao Xie | 03dcc54 | 2013-06-14 01:21:51 -0400 | [diff] [blame^] | 115 | /* PM800: generation identification number */ |
| 116 | #define PM800_CHIP_GEN_ID_NUM 0x3 |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 117 | |
| 118 | static const struct i2c_device_id pm80x_id_table[] = { |
Chao Xie | 03dcc54 | 2013-06-14 01:21:51 -0400 | [diff] [blame^] | 119 | {"88PM800", 0}, |
Samuel Ortiz | 31b3ffb | 2012-07-09 15:11:46 +0200 | [diff] [blame] | 120 | {} /* NULL terminated */ |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 121 | }; |
| 122 | MODULE_DEVICE_TABLE(i2c, pm80x_id_table); |
| 123 | |
| 124 | static struct resource rtc_resources[] = { |
| 125 | { |
| 126 | .name = "88pm80x-rtc", |
| 127 | .start = PM800_IRQ_RTC, |
| 128 | .end = PM800_IRQ_RTC, |
| 129 | .flags = IORESOURCE_IRQ, |
| 130 | }, |
| 131 | }; |
| 132 | |
| 133 | static struct mfd_cell rtc_devs[] = { |
| 134 | { |
| 135 | .name = "88pm80x-rtc", |
| 136 | .num_resources = ARRAY_SIZE(rtc_resources), |
| 137 | .resources = &rtc_resources[0], |
| 138 | .id = -1, |
| 139 | }, |
| 140 | }; |
| 141 | |
| 142 | static struct resource onkey_resources[] = { |
| 143 | { |
| 144 | .name = "88pm80x-onkey", |
| 145 | .start = PM800_IRQ_ONKEY, |
| 146 | .end = PM800_IRQ_ONKEY, |
| 147 | .flags = IORESOURCE_IRQ, |
| 148 | }, |
| 149 | }; |
| 150 | |
| 151 | static struct mfd_cell onkey_devs[] = { |
| 152 | { |
| 153 | .name = "88pm80x-onkey", |
| 154 | .num_resources = 1, |
| 155 | .resources = &onkey_resources[0], |
| 156 | .id = -1, |
| 157 | }, |
| 158 | }; |
| 159 | |
| 160 | static const struct regmap_irq pm800_irqs[] = { |
| 161 | /* INT0 */ |
| 162 | [PM800_IRQ_ONKEY] = { |
| 163 | .mask = PM800_ONKEY_INT_ENA1, |
| 164 | }, |
| 165 | [PM800_IRQ_EXTON] = { |
| 166 | .mask = PM800_EXTON_INT_ENA1, |
| 167 | }, |
| 168 | [PM800_IRQ_CHG] = { |
| 169 | .mask = PM800_CHG_INT_ENA1, |
| 170 | }, |
| 171 | [PM800_IRQ_BAT] = { |
| 172 | .mask = PM800_BAT_INT_ENA1, |
| 173 | }, |
| 174 | [PM800_IRQ_RTC] = { |
| 175 | .mask = PM800_RTC_INT_ENA1, |
| 176 | }, |
| 177 | [PM800_IRQ_CLASSD] = { |
| 178 | .mask = PM800_CLASSD_OC_INT_ENA1, |
| 179 | }, |
| 180 | /* INT1 */ |
| 181 | [PM800_IRQ_VBAT] = { |
| 182 | .reg_offset = 1, |
| 183 | .mask = PM800_VBAT_INT_ENA2, |
| 184 | }, |
| 185 | [PM800_IRQ_VSYS] = { |
| 186 | .reg_offset = 1, |
| 187 | .mask = PM800_VSYS_INT_ENA2, |
| 188 | }, |
| 189 | [PM800_IRQ_VCHG] = { |
| 190 | .reg_offset = 1, |
| 191 | .mask = PM800_VCHG_INT_ENA2, |
| 192 | }, |
| 193 | [PM800_IRQ_TINT] = { |
| 194 | .reg_offset = 1, |
| 195 | .mask = PM800_TINT_INT_ENA2, |
| 196 | }, |
| 197 | /* INT2 */ |
| 198 | [PM800_IRQ_GPADC0] = { |
| 199 | .reg_offset = 2, |
| 200 | .mask = PM800_GPADC0_INT_ENA3, |
| 201 | }, |
| 202 | [PM800_IRQ_GPADC1] = { |
| 203 | .reg_offset = 2, |
| 204 | .mask = PM800_GPADC1_INT_ENA3, |
| 205 | }, |
| 206 | [PM800_IRQ_GPADC2] = { |
| 207 | .reg_offset = 2, |
| 208 | .mask = PM800_GPADC2_INT_ENA3, |
| 209 | }, |
| 210 | [PM800_IRQ_GPADC3] = { |
| 211 | .reg_offset = 2, |
| 212 | .mask = PM800_GPADC3_INT_ENA3, |
| 213 | }, |
| 214 | [PM800_IRQ_GPADC4] = { |
| 215 | .reg_offset = 2, |
| 216 | .mask = PM800_GPADC4_INT_ENA3, |
| 217 | }, |
| 218 | /* INT3 */ |
| 219 | [PM800_IRQ_GPIO0] = { |
| 220 | .reg_offset = 3, |
| 221 | .mask = PM800_GPIO0_INT_ENA4, |
| 222 | }, |
| 223 | [PM800_IRQ_GPIO1] = { |
| 224 | .reg_offset = 3, |
| 225 | .mask = PM800_GPIO1_INT_ENA4, |
| 226 | }, |
| 227 | [PM800_IRQ_GPIO2] = { |
| 228 | .reg_offset = 3, |
| 229 | .mask = PM800_GPIO2_INT_ENA4, |
| 230 | }, |
| 231 | [PM800_IRQ_GPIO3] = { |
| 232 | .reg_offset = 3, |
| 233 | .mask = PM800_GPIO3_INT_ENA4, |
| 234 | }, |
| 235 | [PM800_IRQ_GPIO4] = { |
| 236 | .reg_offset = 3, |
| 237 | .mask = PM800_GPIO4_INT_ENA4, |
| 238 | }, |
| 239 | }; |
| 240 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 241 | static int device_gpadc_init(struct pm80x_chip *chip, |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 242 | struct pm80x_platform_data *pdata) |
| 243 | { |
| 244 | struct pm80x_subchip *subchip = chip->subchip; |
| 245 | struct regmap *map = subchip->regmap_gpadc; |
| 246 | int data = 0, mask = 0, ret = 0; |
| 247 | |
| 248 | if (!map) { |
| 249 | dev_warn(chip->dev, |
| 250 | "Warning: gpadc regmap is not available!\n"); |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | /* |
| 254 | * initialize GPADC without activating it turn on GPADC |
| 255 | * measurments |
| 256 | */ |
| 257 | ret = regmap_update_bits(map, |
| 258 | PM800_GPADC_MISC_CONFIG2, |
| 259 | PM800_GPADC_MISC_GPFSM_EN, |
| 260 | PM800_GPADC_MISC_GPFSM_EN); |
| 261 | if (ret < 0) |
| 262 | goto out; |
| 263 | /* |
| 264 | * This function configures the ADC as requires for |
| 265 | * CP implementation.CP does not "own" the ADC configuration |
| 266 | * registers and relies on AP. |
| 267 | * Reason: enable automatic ADC measurements needed |
| 268 | * for CP to get VBAT and RF temperature readings. |
| 269 | */ |
| 270 | ret = regmap_update_bits(map, PM800_GPADC_MEAS_EN1, |
| 271 | PM800_MEAS_EN1_VBAT, PM800_MEAS_EN1_VBAT); |
| 272 | if (ret < 0) |
| 273 | goto out; |
| 274 | ret = regmap_update_bits(map, PM800_GPADC_MEAS_EN2, |
| 275 | (PM800_MEAS_EN2_RFTMP | PM800_MEAS_GP0_EN), |
| 276 | (PM800_MEAS_EN2_RFTMP | PM800_MEAS_GP0_EN)); |
| 277 | if (ret < 0) |
| 278 | goto out; |
| 279 | |
| 280 | /* |
| 281 | * the defult of PM800 is GPADC operates at 100Ks/s rate |
| 282 | * and Number of GPADC slots with active current bias prior |
| 283 | * to GPADC sampling = 1 slot for all GPADCs set for |
| 284 | * Temprature mesurmants |
| 285 | */ |
| 286 | mask = (PM800_GPADC_GP_BIAS_EN0 | PM800_GPADC_GP_BIAS_EN1 | |
| 287 | PM800_GPADC_GP_BIAS_EN2 | PM800_GPADC_GP_BIAS_EN3); |
| 288 | |
| 289 | if (pdata && (pdata->batt_det == 0)) |
| 290 | data = (PM800_GPADC_GP_BIAS_EN0 | PM800_GPADC_GP_BIAS_EN1 | |
| 291 | PM800_GPADC_GP_BIAS_EN2 | PM800_GPADC_GP_BIAS_EN3); |
| 292 | else |
| 293 | data = (PM800_GPADC_GP_BIAS_EN0 | PM800_GPADC_GP_BIAS_EN2 | |
| 294 | PM800_GPADC_GP_BIAS_EN3); |
| 295 | |
| 296 | ret = regmap_update_bits(map, PM800_GP_BIAS_ENA1, mask, data); |
| 297 | if (ret < 0) |
| 298 | goto out; |
| 299 | |
| 300 | dev_info(chip->dev, "pm800 device_gpadc_init: Done\n"); |
| 301 | return 0; |
| 302 | |
| 303 | out: |
| 304 | dev_info(chip->dev, "pm800 device_gpadc_init: Failed!\n"); |
| 305 | return ret; |
| 306 | } |
| 307 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 308 | static int device_irq_init_800(struct pm80x_chip *chip) |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 309 | { |
| 310 | struct regmap *map = chip->regmap; |
Yi Zhang | 1ef5677 | 2013-06-14 01:21:48 -0400 | [diff] [blame] | 311 | unsigned long flags = IRQF_ONESHOT; |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 312 | int data, mask, ret = -EINVAL; |
| 313 | |
| 314 | if (!map || !chip->irq) { |
| 315 | dev_err(chip->dev, "incorrect parameters\n"); |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * irq_mode defines the way of clearing interrupt. it's read-clear by |
| 321 | * default. |
| 322 | */ |
| 323 | mask = |
| 324 | PM800_WAKEUP2_INV_INT | PM800_WAKEUP2_INT_CLEAR | |
| 325 | PM800_WAKEUP2_INT_MASK; |
| 326 | |
| 327 | data = PM800_WAKEUP2_INT_CLEAR; |
| 328 | ret = regmap_update_bits(map, PM800_WAKEUP2, mask, data); |
| 329 | |
| 330 | if (ret < 0) |
| 331 | goto out; |
| 332 | |
| 333 | ret = |
| 334 | regmap_add_irq_chip(chip->regmap, chip->irq, flags, -1, |
| 335 | chip->regmap_irq_chip, &chip->irq_data); |
| 336 | |
| 337 | out: |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static void device_irq_exit_800(struct pm80x_chip *chip) |
| 342 | { |
| 343 | regmap_del_irq_chip(chip->irq, chip->irq_data); |
| 344 | } |
| 345 | |
| 346 | static struct regmap_irq_chip pm800_irq_chip = { |
| 347 | .name = "88pm800", |
| 348 | .irqs = pm800_irqs, |
| 349 | .num_irqs = ARRAY_SIZE(pm800_irqs), |
| 350 | |
| 351 | .num_regs = 4, |
| 352 | .status_base = PM800_INT_STATUS1, |
| 353 | .mask_base = PM800_INT_ENA_1, |
| 354 | .ack_base = PM800_INT_STATUS1, |
Chao Xie | cb5c580 | 2013-06-14 01:21:47 -0400 | [diff] [blame] | 355 | .mask_invert = 1, |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | static int pm800_pages_init(struct pm80x_chip *chip) |
| 359 | { |
| 360 | struct pm80x_subchip *subchip; |
| 361 | struct i2c_client *client = chip->client; |
| 362 | |
Chao Xie | 5270534 | 2013-06-14 01:21:50 -0400 | [diff] [blame] | 363 | int ret = 0; |
| 364 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 365 | subchip = chip->subchip; |
Chao Xie | 5270534 | 2013-06-14 01:21:50 -0400 | [diff] [blame] | 366 | if (!subchip || !subchip->power_page_addr || !subchip->gpadc_page_addr) |
| 367 | return -ENODEV; |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 368 | |
Chao Xie | 5270534 | 2013-06-14 01:21:50 -0400 | [diff] [blame] | 369 | /* PM800 block power page */ |
| 370 | subchip->power_page = i2c_new_dummy(client->adapter, |
| 371 | subchip->power_page_addr); |
| 372 | if (subchip->power_page == NULL) { |
| 373 | ret = -ENODEV; |
| 374 | goto out; |
| 375 | } |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 376 | |
Chao Xie | 5270534 | 2013-06-14 01:21:50 -0400 | [diff] [blame] | 377 | subchip->regmap_power = devm_regmap_init_i2c(subchip->power_page, |
| 378 | &pm80x_regmap_config); |
| 379 | if (IS_ERR(subchip->regmap_power)) { |
| 380 | ret = PTR_ERR(subchip->regmap_power); |
| 381 | dev_err(chip->dev, |
| 382 | "Failed to allocate regmap_power: %d\n", ret); |
| 383 | goto out; |
| 384 | } |
| 385 | |
| 386 | i2c_set_clientdata(subchip->power_page, chip); |
| 387 | |
| 388 | /* PM800 block GPADC */ |
| 389 | subchip->gpadc_page = i2c_new_dummy(client->adapter, |
| 390 | subchip->gpadc_page_addr); |
| 391 | if (subchip->gpadc_page == NULL) { |
| 392 | ret = -ENODEV; |
| 393 | goto out; |
| 394 | } |
| 395 | |
| 396 | subchip->regmap_gpadc = devm_regmap_init_i2c(subchip->gpadc_page, |
| 397 | &pm80x_regmap_config); |
| 398 | if (IS_ERR(subchip->regmap_gpadc)) { |
| 399 | ret = PTR_ERR(subchip->regmap_gpadc); |
| 400 | dev_err(chip->dev, |
| 401 | "Failed to allocate regmap_gpadc: %d\n", ret); |
| 402 | goto out; |
| 403 | } |
| 404 | i2c_set_clientdata(subchip->gpadc_page, chip); |
| 405 | |
| 406 | out: |
| 407 | return ret; |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static void pm800_pages_exit(struct pm80x_chip *chip) |
| 411 | { |
| 412 | struct pm80x_subchip *subchip; |
| 413 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 414 | subchip = chip->subchip; |
Chao Xie | 5270534 | 2013-06-14 01:21:50 -0400 | [diff] [blame] | 415 | |
| 416 | if (subchip && subchip->power_page) |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 417 | i2c_unregister_device(subchip->power_page); |
Chao Xie | 5270534 | 2013-06-14 01:21:50 -0400 | [diff] [blame] | 418 | |
| 419 | if (subchip && subchip->gpadc_page) |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 420 | i2c_unregister_device(subchip->gpadc_page); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 421 | } |
| 422 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 423 | static int device_800_init(struct pm80x_chip *chip, |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 424 | struct pm80x_platform_data *pdata) |
| 425 | { |
Chao Xie | 03dcc54 | 2013-06-14 01:21:51 -0400 | [diff] [blame^] | 426 | int ret; |
Axel Lin | 46b65a8 | 2012-07-11 09:27:54 +0800 | [diff] [blame] | 427 | unsigned int val; |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 428 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 429 | /* |
| 430 | * alarm wake up bit will be clear in device_irq_init(), |
| 431 | * read before that |
| 432 | */ |
Axel Lin | 46b65a8 | 2012-07-11 09:27:54 +0800 | [diff] [blame] | 433 | ret = regmap_read(chip->regmap, PM800_RTC_CONTROL, &val); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 434 | if (ret < 0) { |
| 435 | dev_err(chip->dev, "Failed to read RTC register: %d\n", ret); |
| 436 | goto out; |
| 437 | } |
Axel Lin | 46b65a8 | 2012-07-11 09:27:54 +0800 | [diff] [blame] | 438 | if (val & PM800_ALARM_WAKEUP) { |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 439 | if (pdata && pdata->rtc) |
| 440 | pdata->rtc->rtc_wakeup = 1; |
| 441 | } |
| 442 | |
| 443 | ret = device_gpadc_init(chip, pdata); |
| 444 | if (ret < 0) { |
| 445 | dev_err(chip->dev, "[%s]Failed to init gpadc\n", __func__); |
| 446 | goto out; |
| 447 | } |
| 448 | |
| 449 | chip->regmap_irq_chip = &pm800_irq_chip; |
| 450 | |
| 451 | ret = device_irq_init_800(chip); |
| 452 | if (ret < 0) { |
| 453 | dev_err(chip->dev, "[%s]Failed to init pm800 irq\n", __func__); |
| 454 | goto out; |
| 455 | } |
| 456 | |
| 457 | ret = |
| 458 | mfd_add_devices(chip->dev, 0, &onkey_devs[0], |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 459 | ARRAY_SIZE(onkey_devs), &onkey_resources[0], 0, |
| 460 | NULL); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 461 | if (ret < 0) { |
| 462 | dev_err(chip->dev, "Failed to add onkey subdev\n"); |
| 463 | goto out_dev; |
| 464 | } else |
| 465 | dev_info(chip->dev, "[%s]:Added mfd onkey_devs\n", __func__); |
| 466 | |
| 467 | if (pdata && pdata->rtc) { |
| 468 | rtc_devs[0].platform_data = pdata->rtc; |
| 469 | rtc_devs[0].pdata_size = sizeof(struct pm80x_rtc_pdata); |
| 470 | ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0], |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 471 | ARRAY_SIZE(rtc_devs), NULL, 0, NULL); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 472 | if (ret < 0) { |
| 473 | dev_err(chip->dev, "Failed to add rtc subdev\n"); |
| 474 | goto out_dev; |
| 475 | } else |
| 476 | dev_info(chip->dev, |
| 477 | "[%s]:Added mfd rtc_devs\n", __func__); |
| 478 | } |
| 479 | |
| 480 | return 0; |
| 481 | out_dev: |
| 482 | mfd_remove_devices(chip->dev); |
| 483 | device_irq_exit_800(chip); |
| 484 | out: |
| 485 | return ret; |
| 486 | } |
| 487 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 488 | static int pm800_probe(struct i2c_client *client, |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 489 | const struct i2c_device_id *id) |
| 490 | { |
| 491 | int ret = 0; |
| 492 | struct pm80x_chip *chip; |
| 493 | struct pm80x_platform_data *pdata = client->dev.platform_data; |
| 494 | struct pm80x_subchip *subchip; |
| 495 | |
Chao Xie | 03dcc54 | 2013-06-14 01:21:51 -0400 | [diff] [blame^] | 496 | ret = pm80x_init(client); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 497 | if (ret) { |
| 498 | dev_err(&client->dev, "pm800_init fail\n"); |
| 499 | goto out_init; |
| 500 | } |
| 501 | |
| 502 | chip = i2c_get_clientdata(client); |
| 503 | |
| 504 | /* init subchip for PM800 */ |
| 505 | subchip = |
| 506 | devm_kzalloc(&client->dev, sizeof(struct pm80x_subchip), |
| 507 | GFP_KERNEL); |
| 508 | if (!subchip) { |
| 509 | ret = -ENOMEM; |
| 510 | goto err_subchip_alloc; |
| 511 | } |
| 512 | |
Chao Xie | c750d8e | 2013-06-14 01:21:49 -0400 | [diff] [blame] | 513 | /* pm800 has 2 addtional pages to support power and gpadc. */ |
| 514 | subchip->power_page_addr = client->addr + 1; |
| 515 | subchip->gpadc_page_addr = client->addr + 2; |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 516 | chip->subchip = subchip; |
| 517 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 518 | ret = pm800_pages_init(chip); |
| 519 | if (ret) { |
| 520 | dev_err(&client->dev, "pm800_pages_init failed!\n"); |
| 521 | goto err_page_init; |
| 522 | } |
| 523 | |
Yi Zhang | 618fa57 | 2013-06-14 01:21:45 -0400 | [diff] [blame] | 524 | ret = device_800_init(chip, pdata); |
| 525 | if (ret) { |
Chao Xie | 03dcc54 | 2013-06-14 01:21:51 -0400 | [diff] [blame^] | 526 | dev_err(chip->dev, "Failed to initialize 88pm800 devices\n"); |
Yi Zhang | 618fa57 | 2013-06-14 01:21:45 -0400 | [diff] [blame] | 527 | goto err_device_init; |
| 528 | } |
| 529 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 530 | if (pdata->plat_config) |
| 531 | pdata->plat_config(chip, pdata); |
| 532 | |
Yi Zhang | 618fa57 | 2013-06-14 01:21:45 -0400 | [diff] [blame] | 533 | return 0; |
| 534 | |
| 535 | err_device_init: |
| 536 | pm800_pages_exit(chip); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 537 | err_page_init: |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 538 | err_subchip_alloc: |
Yi Zhang | 306df79 | 2013-01-22 10:43:45 +0800 | [diff] [blame] | 539 | pm80x_deinit(); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 540 | out_init: |
| 541 | return ret; |
| 542 | } |
| 543 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 544 | static int pm800_remove(struct i2c_client *client) |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 545 | { |
| 546 | struct pm80x_chip *chip = i2c_get_clientdata(client); |
| 547 | |
| 548 | mfd_remove_devices(chip->dev); |
| 549 | device_irq_exit_800(chip); |
| 550 | |
| 551 | pm800_pages_exit(chip); |
Yi Zhang | 306df79 | 2013-01-22 10:43:45 +0800 | [diff] [blame] | 552 | pm80x_deinit(); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static struct i2c_driver pm800_driver = { |
| 558 | .driver = { |
Chao Xie | 46223a1 | 2013-06-14 01:21:46 -0400 | [diff] [blame] | 559 | .name = "88PM800", |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 560 | .owner = THIS_MODULE, |
| 561 | .pm = &pm80x_pm_ops, |
| 562 | }, |
| 563 | .probe = pm800_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 564 | .remove = pm800_remove, |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 565 | .id_table = pm80x_id_table, |
| 566 | }; |
| 567 | |
| 568 | static int __init pm800_i2c_init(void) |
| 569 | { |
| 570 | return i2c_add_driver(&pm800_driver); |
| 571 | } |
| 572 | subsys_initcall(pm800_i2c_init); |
| 573 | |
| 574 | static void __exit pm800_i2c_exit(void) |
| 575 | { |
| 576 | i2c_del_driver(&pm800_driver); |
| 577 | } |
| 578 | module_exit(pm800_i2c_exit); |
| 579 | |
| 580 | MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM800"); |
| 581 | MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>"); |
| 582 | MODULE_LICENSE("GPL"); |