1 /* 2 * TI Palmas MFD Driver 3 * 4 * Copyright 2011-2012 Texas Instruments Inc. 5 * 6 * Author: Graeme Gregory <gg@slimlogic.co.uk> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2 of the License, or (at your 11 * option) any later version. 12 * 13 */ 14 15 #include <linux/module.h> 16 #include <linux/moduleparam.h> 17 #include <linux/init.h> 18 #include <linux/slab.h> 19 #include <linux/i2c.h> 20 #include <linux/interrupt.h> 21 #include <linux/irq.h> 22 #include <linux/regmap.h> 23 #include <linux/err.h> 24 #include <linux/mfd/core.h> 25 #include <linux/mfd/palmas.h> 26 #include <linux/of_platform.h> 27 28 enum palmas_ids { 29 PALMAS_PMIC_ID, 30 PALMAS_GPIO_ID, 31 PALMAS_LEDS_ID, 32 PALMAS_WDT_ID, 33 PALMAS_RTC_ID, 34 PALMAS_PWRBUTTON_ID, 35 PALMAS_GPADC_ID, 36 PALMAS_RESOURCE_ID, 37 PALMAS_CLK_ID, 38 PALMAS_PWM_ID, 39 PALMAS_USB_ID, 40 }; 41 42 static struct resource palmas_rtc_resources[] = { 43 { 44 .start = PALMAS_RTC_ALARM_IRQ, 45 .end = PALMAS_RTC_ALARM_IRQ, 46 .flags = IORESOURCE_IRQ, 47 }, 48 }; 49 50 static const struct mfd_cell palmas_children[] = { 51 { 52 .name = "palmas-pmic", 53 .id = PALMAS_PMIC_ID, 54 }, 55 { 56 .name = "palmas-gpio", 57 .id = PALMAS_GPIO_ID, 58 }, 59 { 60 .name = "palmas-leds", 61 .id = PALMAS_LEDS_ID, 62 }, 63 { 64 .name = "palmas-wdt", 65 .id = PALMAS_WDT_ID, 66 }, 67 { 68 .name = "palmas-rtc", 69 .id = PALMAS_RTC_ID, 70 .resources = &palmas_rtc_resources[0], 71 .num_resources = ARRAY_SIZE(palmas_rtc_resources), 72 }, 73 { 74 .name = "palmas-pwrbutton", 75 .id = PALMAS_PWRBUTTON_ID, 76 }, 77 { 78 .name = "palmas-gpadc", 79 .id = PALMAS_GPADC_ID, 80 }, 81 { 82 .name = "palmas-resource", 83 .id = PALMAS_RESOURCE_ID, 84 }, 85 { 86 .name = "palmas-clk", 87 .id = PALMAS_CLK_ID, 88 }, 89 { 90 .name = "palmas-pwm", 91 .id = PALMAS_PWM_ID, 92 }, 93 { 94 .name = "palmas-usb", 95 .id = PALMAS_USB_ID, 96 } 97 }; 98 99 static const struct regmap_config palmas_regmap_config[PALMAS_NUM_CLIENTS] = { 100 { 101 .reg_bits = 8, 102 .val_bits = 8, 103 .max_register = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE, 104 PALMAS_PRIMARY_SECONDARY_PAD3), 105 }, 106 { 107 .reg_bits = 8, 108 .val_bits = 8, 109 .max_register = PALMAS_BASE_TO_REG(PALMAS_GPADC_BASE, 110 PALMAS_GPADC_SMPS_VSEL_MONITORING), 111 }, 112 { 113 .reg_bits = 8, 114 .val_bits = 8, 115 .max_register = PALMAS_BASE_TO_REG(PALMAS_TRIM_GPADC_BASE, 116 PALMAS_GPADC_TRIM16), 117 }, 118 }; 119 120 static const struct regmap_irq palmas_irqs[] = { 121 /* INT1 IRQs */ 122 [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ] = { 123 .mask = PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV, 124 }, 125 [PALMAS_PWRON_IRQ] = { 126 .mask = PALMAS_INT1_STATUS_PWRON, 127 }, 128 [PALMAS_LONG_PRESS_KEY_IRQ] = { 129 .mask = PALMAS_INT1_STATUS_LONG_PRESS_KEY, 130 }, 131 [PALMAS_RPWRON_IRQ] = { 132 .mask = PALMAS_INT1_STATUS_RPWRON, 133 }, 134 [PALMAS_PWRDOWN_IRQ] = { 135 .mask = PALMAS_INT1_STATUS_PWRDOWN, 136 }, 137 [PALMAS_HOTDIE_IRQ] = { 138 .mask = PALMAS_INT1_STATUS_HOTDIE, 139 }, 140 [PALMAS_VSYS_MON_IRQ] = { 141 .mask = PALMAS_INT1_STATUS_VSYS_MON, 142 }, 143 [PALMAS_VBAT_MON_IRQ] = { 144 .mask = PALMAS_INT1_STATUS_VBAT_MON, 145 }, 146 /* INT2 IRQs*/ 147 [PALMAS_RTC_ALARM_IRQ] = { 148 .mask = PALMAS_INT2_STATUS_RTC_ALARM, 149 .reg_offset = 1, 150 }, 151 [PALMAS_RTC_TIMER_IRQ] = { 152 .mask = PALMAS_INT2_STATUS_RTC_TIMER, 153 .reg_offset = 1, 154 }, 155 [PALMAS_WDT_IRQ] = { 156 .mask = PALMAS_INT2_STATUS_WDT, 157 .reg_offset = 1, 158 }, 159 [PALMAS_BATREMOVAL_IRQ] = { 160 .mask = PALMAS_INT2_STATUS_BATREMOVAL, 161 .reg_offset = 1, 162 }, 163 [PALMAS_RESET_IN_IRQ] = { 164 .mask = PALMAS_INT2_STATUS_RESET_IN, 165 .reg_offset = 1, 166 }, 167 [PALMAS_FBI_BB_IRQ] = { 168 .mask = PALMAS_INT2_STATUS_FBI_BB, 169 .reg_offset = 1, 170 }, 171 [PALMAS_SHORT_IRQ] = { 172 .mask = PALMAS_INT2_STATUS_SHORT, 173 .reg_offset = 1, 174 }, 175 [PALMAS_VAC_ACOK_IRQ] = { 176 .mask = PALMAS_INT2_STATUS_VAC_ACOK, 177 .reg_offset = 1, 178 }, 179 /* INT3 IRQs */ 180 [PALMAS_GPADC_AUTO_0_IRQ] = { 181 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_0, 182 .reg_offset = 2, 183 }, 184 [PALMAS_GPADC_AUTO_1_IRQ] = { 185 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_1, 186 .reg_offset = 2, 187 }, 188 [PALMAS_GPADC_EOC_SW_IRQ] = { 189 .mask = PALMAS_INT3_STATUS_GPADC_EOC_SW, 190 .reg_offset = 2, 191 }, 192 [PALMAS_GPADC_EOC_RT_IRQ] = { 193 .mask = PALMAS_INT3_STATUS_GPADC_EOC_RT, 194 .reg_offset = 2, 195 }, 196 [PALMAS_ID_OTG_IRQ] = { 197 .mask = PALMAS_INT3_STATUS_ID_OTG, 198 .reg_offset = 2, 199 }, 200 [PALMAS_ID_IRQ] = { 201 .mask = PALMAS_INT3_STATUS_ID, 202 .reg_offset = 2, 203 }, 204 [PALMAS_VBUS_OTG_IRQ] = { 205 .mask = PALMAS_INT3_STATUS_VBUS_OTG, 206 .reg_offset = 2, 207 }, 208 [PALMAS_VBUS_IRQ] = { 209 .mask = PALMAS_INT3_STATUS_VBUS, 210 .reg_offset = 2, 211 }, 212 /* INT4 IRQs */ 213 [PALMAS_GPIO_0_IRQ] = { 214 .mask = PALMAS_INT4_STATUS_GPIO_0, 215 .reg_offset = 3, 216 }, 217 [PALMAS_GPIO_1_IRQ] = { 218 .mask = PALMAS_INT4_STATUS_GPIO_1, 219 .reg_offset = 3, 220 }, 221 [PALMAS_GPIO_2_IRQ] = { 222 .mask = PALMAS_INT4_STATUS_GPIO_2, 223 .reg_offset = 3, 224 }, 225 [PALMAS_GPIO_3_IRQ] = { 226 .mask = PALMAS_INT4_STATUS_GPIO_3, 227 .reg_offset = 3, 228 }, 229 [PALMAS_GPIO_4_IRQ] = { 230 .mask = PALMAS_INT4_STATUS_GPIO_4, 231 .reg_offset = 3, 232 }, 233 [PALMAS_GPIO_5_IRQ] = { 234 .mask = PALMAS_INT4_STATUS_GPIO_5, 235 .reg_offset = 3, 236 }, 237 [PALMAS_GPIO_6_IRQ] = { 238 .mask = PALMAS_INT4_STATUS_GPIO_6, 239 .reg_offset = 3, 240 }, 241 [PALMAS_GPIO_7_IRQ] = { 242 .mask = PALMAS_INT4_STATUS_GPIO_7, 243 .reg_offset = 3, 244 }, 245 }; 246 247 static struct regmap_irq_chip palmas_irq_chip = { 248 .name = "palmas", 249 .irqs = palmas_irqs, 250 .num_irqs = ARRAY_SIZE(palmas_irqs), 251 252 .num_regs = 4, 253 .irq_reg_stride = 5, 254 .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, 255 PALMAS_INT1_STATUS), 256 .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, 257 PALMAS_INT1_MASK), 258 }; 259 260 static int palmas_set_pdata_irq_flag(struct i2c_client *i2c, 261 struct palmas_platform_data *pdata) 262 { 263 struct irq_data *irq_data = irq_get_irq_data(i2c->irq); 264 if (!irq_data) { 265 dev_err(&i2c->dev, "Invalid IRQ: %d\n", i2c->irq); 266 return -EINVAL; 267 } 268 269 pdata->irq_flags = irqd_get_trigger_type(irq_data); 270 dev_info(&i2c->dev, "Irq flag is 0x%08x\n", pdata->irq_flags); 271 return 0; 272 } 273 274 static void palmas_dt_to_pdata(struct i2c_client *i2c, 275 struct palmas_platform_data *pdata) 276 { 277 struct device_node *node = i2c->dev.of_node; 278 int ret; 279 u32 prop; 280 281 ret = of_property_read_u32(node, "ti,mux-pad1", &prop); 282 if (!ret) { 283 pdata->mux_from_pdata = 1; 284 pdata->pad1 = prop; 285 } 286 287 ret = of_property_read_u32(node, "ti,mux-pad2", &prop); 288 if (!ret) { 289 pdata->mux_from_pdata = 1; 290 pdata->pad2 = prop; 291 } 292 293 /* The default for this register is all masked */ 294 ret = of_property_read_u32(node, "ti,power-ctrl", &prop); 295 if (!ret) 296 pdata->power_ctrl = prop; 297 else 298 pdata->power_ctrl = PALMAS_POWER_CTRL_NSLEEP_MASK | 299 PALMAS_POWER_CTRL_ENABLE1_MASK | 300 PALMAS_POWER_CTRL_ENABLE2_MASK; 301 if (i2c->irq) 302 palmas_set_pdata_irq_flag(i2c, pdata); 303 } 304 305 static int palmas_i2c_probe(struct i2c_client *i2c, 306 const struct i2c_device_id *id) 307 { 308 struct palmas *palmas; 309 struct palmas_platform_data *pdata; 310 struct device_node *node = i2c->dev.of_node; 311 int ret = 0, i; 312 unsigned int reg, addr; 313 int slave; 314 struct mfd_cell *children; 315 316 pdata = dev_get_platdata(&i2c->dev); 317 318 if (node && !pdata) { 319 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL); 320 321 if (!pdata) 322 return -ENOMEM; 323 324 palmas_dt_to_pdata(i2c, pdata); 325 } 326 327 if (!pdata) 328 return -EINVAL; 329 330 palmas = devm_kzalloc(&i2c->dev, sizeof(struct palmas), GFP_KERNEL); 331 if (palmas == NULL) 332 return -ENOMEM; 333 334 i2c_set_clientdata(i2c, palmas); 335 palmas->dev = &i2c->dev; 336 palmas->id = id->driver_data; 337 palmas->irq = i2c->irq; 338 339 for (i = 0; i < PALMAS_NUM_CLIENTS; i++) { 340 if (i == 0) 341 palmas->i2c_clients[i] = i2c; 342 else { 343 palmas->i2c_clients[i] = 344 i2c_new_dummy(i2c->adapter, 345 i2c->addr + i); 346 if (!palmas->i2c_clients[i]) { 347 dev_err(palmas->dev, 348 "can't attach client %d\n", i); 349 ret = -ENOMEM; 350 goto err; 351 } 352 palmas->i2c_clients[i]->dev.of_node = of_node_get(node); 353 } 354 palmas->regmap[i] = devm_regmap_init_i2c(palmas->i2c_clients[i], 355 &palmas_regmap_config[i]); 356 if (IS_ERR(palmas->regmap[i])) { 357 ret = PTR_ERR(palmas->regmap[i]); 358 dev_err(palmas->dev, 359 "Failed to allocate regmap %d, err: %d\n", 360 i, ret); 361 goto err; 362 } 363 } 364 365 /* Change interrupt line output polarity */ 366 if (pdata->irq_flags & IRQ_TYPE_LEVEL_HIGH) 367 reg = PALMAS_POLARITY_CTRL_INT_POLARITY; 368 else 369 reg = 0; 370 ret = palmas_update_bits(palmas, PALMAS_PU_PD_OD_BASE, 371 PALMAS_POLARITY_CTRL, PALMAS_POLARITY_CTRL_INT_POLARITY, 372 reg); 373 if (ret < 0) { 374 dev_err(palmas->dev, "POLARITY_CTRL updat failed: %d\n", ret); 375 goto err; 376 } 377 378 /* Change IRQ into clear on read mode for efficiency */ 379 slave = PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE); 380 addr = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, PALMAS_INT_CTRL); 381 reg = PALMAS_INT_CTRL_INT_CLEAR; 382 383 regmap_write(palmas->regmap[slave], addr, reg); 384 385 ret = regmap_add_irq_chip(palmas->regmap[slave], palmas->irq, 386 IRQF_ONESHOT | pdata->irq_flags, 0, &palmas_irq_chip, 387 &palmas->irq_data); 388 if (ret < 0) 389 goto err; 390 391 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE); 392 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE, 393 PALMAS_PRIMARY_SECONDARY_PAD1); 394 395 if (pdata->mux_from_pdata) { 396 reg = pdata->pad1; 397 ret = regmap_write(palmas->regmap[slave], addr, reg); 398 if (ret) 399 goto err_irq; 400 } else { 401 ret = regmap_read(palmas->regmap[slave], addr, ®); 402 if (ret) 403 goto err_irq; 404 } 405 406 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0)) 407 palmas->gpio_muxed |= PALMAS_GPIO_0_MUXED; 408 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK)) 409 palmas->gpio_muxed |= PALMAS_GPIO_1_MUXED; 410 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) == 411 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT)) 412 palmas->led_muxed |= PALMAS_LED1_MUXED; 413 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) == 414 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT)) 415 palmas->pwm_muxed |= PALMAS_PWM1_MUXED; 416 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK)) 417 palmas->gpio_muxed |= PALMAS_GPIO_2_MUXED; 418 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) == 419 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT)) 420 palmas->led_muxed |= PALMAS_LED2_MUXED; 421 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) == 422 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT)) 423 palmas->pwm_muxed |= PALMAS_PWM2_MUXED; 424 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3)) 425 palmas->gpio_muxed |= PALMAS_GPIO_3_MUXED; 426 427 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE, 428 PALMAS_PRIMARY_SECONDARY_PAD2); 429 430 if (pdata->mux_from_pdata) { 431 reg = pdata->pad2; 432 ret = regmap_write(palmas->regmap[slave], addr, reg); 433 if (ret) 434 goto err_irq; 435 } else { 436 ret = regmap_read(palmas->regmap[slave], addr, ®); 437 if (ret) 438 goto err_irq; 439 } 440 441 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4)) 442 palmas->gpio_muxed |= PALMAS_GPIO_4_MUXED; 443 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK)) 444 palmas->gpio_muxed |= PALMAS_GPIO_5_MUXED; 445 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6)) 446 palmas->gpio_muxed |= PALMAS_GPIO_6_MUXED; 447 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK)) 448 palmas->gpio_muxed |= PALMAS_GPIO_7_MUXED; 449 450 dev_info(palmas->dev, "Muxing GPIO %x, PWM %x, LED %x\n", 451 palmas->gpio_muxed, palmas->pwm_muxed, 452 palmas->led_muxed); 453 454 reg = pdata->power_ctrl; 455 456 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE); 457 addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_POWER_CTRL); 458 459 ret = regmap_write(palmas->regmap[slave], addr, reg); 460 if (ret) 461 goto err_irq; 462 463 /* 464 * If we are probing with DT do this the DT way and return here 465 * otherwise continue and add devices using mfd helpers. 466 */ 467 if (node) { 468 ret = of_platform_populate(node, NULL, NULL, &i2c->dev); 469 if (ret < 0) 470 goto err_irq; 471 else 472 return ret; 473 } 474 475 children = kmemdup(palmas_children, sizeof(palmas_children), 476 GFP_KERNEL); 477 if (!children) { 478 ret = -ENOMEM; 479 goto err_irq; 480 } 481 482 children[PALMAS_PMIC_ID].platform_data = pdata->pmic_pdata; 483 children[PALMAS_PMIC_ID].pdata_size = sizeof(*pdata->pmic_pdata); 484 485 children[PALMAS_GPADC_ID].platform_data = pdata->gpadc_pdata; 486 children[PALMAS_GPADC_ID].pdata_size = sizeof(*pdata->gpadc_pdata); 487 488 children[PALMAS_RESOURCE_ID].platform_data = pdata->resource_pdata; 489 children[PALMAS_RESOURCE_ID].pdata_size = 490 sizeof(*pdata->resource_pdata); 491 492 children[PALMAS_USB_ID].platform_data = pdata->usb_pdata; 493 children[PALMAS_USB_ID].pdata_size = sizeof(*pdata->usb_pdata); 494 495 children[PALMAS_CLK_ID].platform_data = pdata->clk_pdata; 496 children[PALMAS_CLK_ID].pdata_size = sizeof(*pdata->clk_pdata); 497 498 ret = mfd_add_devices(palmas->dev, -1, 499 children, ARRAY_SIZE(palmas_children), 500 NULL, 0, 501 regmap_irq_get_domain(palmas->irq_data)); 502 kfree(children); 503 504 if (ret < 0) 505 goto err_devices; 506 507 return ret; 508 509 err_devices: 510 mfd_remove_devices(palmas->dev); 511 err_irq: 512 regmap_del_irq_chip(palmas->irq, palmas->irq_data); 513 err: 514 return ret; 515 } 516 517 static int palmas_i2c_remove(struct i2c_client *i2c) 518 { 519 struct palmas *palmas = i2c_get_clientdata(i2c); 520 521 mfd_remove_devices(palmas->dev); 522 regmap_del_irq_chip(palmas->irq, palmas->irq_data); 523 524 return 0; 525 } 526 527 static const struct i2c_device_id palmas_i2c_id[] = { 528 { "palmas", }, 529 { "twl6035", }, 530 { "twl6037", }, 531 { "tps65913", }, 532 { /* end */ } 533 }; 534 MODULE_DEVICE_TABLE(i2c, palmas_i2c_id); 535 536 static struct of_device_id of_palmas_match_tbl[] = { 537 { .compatible = "ti,palmas", }, 538 { /* end */ } 539 }; 540 541 static struct i2c_driver palmas_i2c_driver = { 542 .driver = { 543 .name = "palmas", 544 .of_match_table = of_palmas_match_tbl, 545 .owner = THIS_MODULE, 546 }, 547 .probe = palmas_i2c_probe, 548 .remove = palmas_i2c_remove, 549 .id_table = palmas_i2c_id, 550 }; 551 552 static int __init palmas_i2c_init(void) 553 { 554 return i2c_add_driver(&palmas_i2c_driver); 555 } 556 /* init early so consumer devices can complete system boot */ 557 subsys_initcall(palmas_i2c_init); 558 559 static void __exit palmas_i2c_exit(void) 560 { 561 i2c_del_driver(&palmas_i2c_driver); 562 } 563 module_exit(palmas_i2c_exit); 564 565 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>"); 566 MODULE_DESCRIPTION("Palmas chip family multi-function driver"); 567 MODULE_LICENSE("GPL"); 568