4 * Copyright 2011-2012 Texas Instruments Inc.
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
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.
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>
42 static struct resource palmas_rtc_resources[] = {
44 .start = PALMAS_RTC_ALARM_IRQ,
45 .end = PALMAS_RTC_ALARM_IRQ,
46 .flags = IORESOURCE_IRQ,
50 static const struct mfd_cell palmas_children[] = {
52 .name = "palmas-pmic",
56 .name = "palmas-gpio",
60 .name = "palmas-leds",
70 .resources = &palmas_rtc_resources[0],
71 .num_resources = ARRAY_SIZE(palmas_rtc_resources),
74 .name = "palmas-pwrbutton",
75 .id = PALMAS_PWRBUTTON_ID,
78 .name = "palmas-gpadc",
79 .id = PALMAS_GPADC_ID,
82 .name = "palmas-resource",
83 .id = PALMAS_RESOURCE_ID,
99 static const struct regmap_config palmas_regmap_config[PALMAS_NUM_CLIENTS] = {
103 .max_register = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
104 PALMAS_PRIMARY_SECONDARY_PAD3),
109 .max_register = PALMAS_BASE_TO_REG(PALMAS_GPADC_BASE,
110 PALMAS_GPADC_SMPS_VSEL_MONITORING),
115 .max_register = PALMAS_BASE_TO_REG(PALMAS_TRIM_GPADC_BASE,
116 PALMAS_GPADC_TRIM16),
120 static const struct regmap_irq palmas_irqs[] = {
122 [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ] = {
123 .mask = PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV,
125 [PALMAS_PWRON_IRQ] = {
126 .mask = PALMAS_INT1_STATUS_PWRON,
128 [PALMAS_LONG_PRESS_KEY_IRQ] = {
129 .mask = PALMAS_INT1_STATUS_LONG_PRESS_KEY,
131 [PALMAS_RPWRON_IRQ] = {
132 .mask = PALMAS_INT1_STATUS_RPWRON,
134 [PALMAS_PWRDOWN_IRQ] = {
135 .mask = PALMAS_INT1_STATUS_PWRDOWN,
137 [PALMAS_HOTDIE_IRQ] = {
138 .mask = PALMAS_INT1_STATUS_HOTDIE,
140 [PALMAS_VSYS_MON_IRQ] = {
141 .mask = PALMAS_INT1_STATUS_VSYS_MON,
143 [PALMAS_VBAT_MON_IRQ] = {
144 .mask = PALMAS_INT1_STATUS_VBAT_MON,
147 [PALMAS_RTC_ALARM_IRQ] = {
148 .mask = PALMAS_INT2_STATUS_RTC_ALARM,
151 [PALMAS_RTC_TIMER_IRQ] = {
152 .mask = PALMAS_INT2_STATUS_RTC_TIMER,
156 .mask = PALMAS_INT2_STATUS_WDT,
159 [PALMAS_BATREMOVAL_IRQ] = {
160 .mask = PALMAS_INT2_STATUS_BATREMOVAL,
163 [PALMAS_RESET_IN_IRQ] = {
164 .mask = PALMAS_INT2_STATUS_RESET_IN,
167 [PALMAS_FBI_BB_IRQ] = {
168 .mask = PALMAS_INT2_STATUS_FBI_BB,
171 [PALMAS_SHORT_IRQ] = {
172 .mask = PALMAS_INT2_STATUS_SHORT,
175 [PALMAS_VAC_ACOK_IRQ] = {
176 .mask = PALMAS_INT2_STATUS_VAC_ACOK,
180 [PALMAS_GPADC_AUTO_0_IRQ] = {
181 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_0,
184 [PALMAS_GPADC_AUTO_1_IRQ] = {
185 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_1,
188 [PALMAS_GPADC_EOC_SW_IRQ] = {
189 .mask = PALMAS_INT3_STATUS_GPADC_EOC_SW,
192 [PALMAS_GPADC_EOC_RT_IRQ] = {
193 .mask = PALMAS_INT3_STATUS_GPADC_EOC_RT,
196 [PALMAS_ID_OTG_IRQ] = {
197 .mask = PALMAS_INT3_STATUS_ID_OTG,
201 .mask = PALMAS_INT3_STATUS_ID,
204 [PALMAS_VBUS_OTG_IRQ] = {
205 .mask = PALMAS_INT3_STATUS_VBUS_OTG,
208 [PALMAS_VBUS_IRQ] = {
209 .mask = PALMAS_INT3_STATUS_VBUS,
213 [PALMAS_GPIO_0_IRQ] = {
214 .mask = PALMAS_INT4_STATUS_GPIO_0,
217 [PALMAS_GPIO_1_IRQ] = {
218 .mask = PALMAS_INT4_STATUS_GPIO_1,
221 [PALMAS_GPIO_2_IRQ] = {
222 .mask = PALMAS_INT4_STATUS_GPIO_2,
225 [PALMAS_GPIO_3_IRQ] = {
226 .mask = PALMAS_INT4_STATUS_GPIO_3,
229 [PALMAS_GPIO_4_IRQ] = {
230 .mask = PALMAS_INT4_STATUS_GPIO_4,
233 [PALMAS_GPIO_5_IRQ] = {
234 .mask = PALMAS_INT4_STATUS_GPIO_5,
237 [PALMAS_GPIO_6_IRQ] = {
238 .mask = PALMAS_INT4_STATUS_GPIO_6,
241 [PALMAS_GPIO_7_IRQ] = {
242 .mask = PALMAS_INT4_STATUS_GPIO_7,
247 static struct regmap_irq_chip palmas_irq_chip = {
250 .num_irqs = ARRAY_SIZE(palmas_irqs),
254 .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
256 .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
260 static int palmas_set_pdata_irq_flag(struct i2c_client *i2c,
261 struct palmas_platform_data *pdata)
263 struct irq_data *irq_data = irq_get_irq_data(i2c->irq);
265 dev_err(&i2c->dev, "Invalid IRQ: %d\n", i2c->irq);
269 pdata->irq_flags = irqd_get_trigger_type(irq_data);
270 dev_info(&i2c->dev, "Irq flag is 0x%08x\n", pdata->irq_flags);
274 static void palmas_dt_to_pdata(struct i2c_client *i2c,
275 struct palmas_platform_data *pdata)
277 struct device_node *node = i2c->dev.of_node;
281 ret = of_property_read_u32(node, "ti,mux-pad1", &prop);
283 pdata->mux_from_pdata = 1;
287 ret = of_property_read_u32(node, "ti,mux-pad2", &prop);
289 pdata->mux_from_pdata = 1;
293 /* The default for this register is all masked */
294 ret = of_property_read_u32(node, "ti,power-ctrl", &prop);
296 pdata->power_ctrl = prop;
298 pdata->power_ctrl = PALMAS_POWER_CTRL_NSLEEP_MASK |
299 PALMAS_POWER_CTRL_ENABLE1_MASK |
300 PALMAS_POWER_CTRL_ENABLE2_MASK;
302 palmas_set_pdata_irq_flag(i2c, pdata);
305 static int palmas_i2c_probe(struct i2c_client *i2c,
306 const struct i2c_device_id *id)
308 struct palmas *palmas;
309 struct palmas_platform_data *pdata;
310 struct device_node *node = i2c->dev.of_node;
312 unsigned int reg, addr;
314 struct mfd_cell *children;
316 pdata = dev_get_platdata(&i2c->dev);
318 if (node && !pdata) {
319 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
324 palmas_dt_to_pdata(i2c, pdata);
330 palmas = devm_kzalloc(&i2c->dev, sizeof(struct palmas), GFP_KERNEL);
334 i2c_set_clientdata(i2c, palmas);
335 palmas->dev = &i2c->dev;
336 palmas->id = id->driver_data;
337 palmas->irq = i2c->irq;
339 for (i = 0; i < PALMAS_NUM_CLIENTS; i++) {
341 palmas->i2c_clients[i] = i2c;
343 palmas->i2c_clients[i] =
344 i2c_new_dummy(i2c->adapter,
346 if (!palmas->i2c_clients[i]) {
348 "can't attach client %d\n", i);
352 palmas->i2c_clients[i]->dev.of_node = of_node_get(node);
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]);
359 "Failed to allocate regmap %d, err: %d\n",
365 /* Change interrupt line output polarity */
366 if (pdata->irq_flags & IRQ_TYPE_LEVEL_HIGH)
367 reg = PALMAS_POLARITY_CTRL_INT_POLARITY;
370 ret = palmas_update_bits(palmas, PALMAS_PU_PD_OD_BASE,
371 PALMAS_POLARITY_CTRL, PALMAS_POLARITY_CTRL_INT_POLARITY,
374 dev_err(palmas->dev, "POLARITY_CTRL updat failed: %d\n", ret);
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;
383 regmap_write(palmas->regmap[slave], addr, reg);
385 ret = regmap_add_irq_chip(palmas->regmap[slave], palmas->irq,
386 IRQF_ONESHOT | pdata->irq_flags, 0, &palmas_irq_chip,
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);
395 if (pdata->mux_from_pdata) {
397 ret = regmap_write(palmas->regmap[slave], addr, reg);
401 ret = regmap_read(palmas->regmap[slave], addr, ®);
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;
427 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
428 PALMAS_PRIMARY_SECONDARY_PAD2);
430 if (pdata->mux_from_pdata) {
432 ret = regmap_write(palmas->regmap[slave], addr, reg);
436 ret = regmap_read(palmas->regmap[slave], addr, ®);
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;
450 dev_info(palmas->dev, "Muxing GPIO %x, PWM %x, LED %x\n",
451 palmas->gpio_muxed, palmas->pwm_muxed,
454 reg = pdata->power_ctrl;
456 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
457 addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_POWER_CTRL);
459 ret = regmap_write(palmas->regmap[slave], addr, reg);
464 * If we are probing with DT do this the DT way and return here
465 * otherwise continue and add devices using mfd helpers.
468 ret = of_platform_populate(node, NULL, NULL, &i2c->dev);
475 children = kmemdup(palmas_children, sizeof(palmas_children),
482 children[PALMAS_PMIC_ID].platform_data = pdata->pmic_pdata;
483 children[PALMAS_PMIC_ID].pdata_size = sizeof(*pdata->pmic_pdata);
485 children[PALMAS_GPADC_ID].platform_data = pdata->gpadc_pdata;
486 children[PALMAS_GPADC_ID].pdata_size = sizeof(*pdata->gpadc_pdata);
488 children[PALMAS_RESOURCE_ID].platform_data = pdata->resource_pdata;
489 children[PALMAS_RESOURCE_ID].pdata_size =
490 sizeof(*pdata->resource_pdata);
492 children[PALMAS_USB_ID].platform_data = pdata->usb_pdata;
493 children[PALMAS_USB_ID].pdata_size = sizeof(*pdata->usb_pdata);
495 children[PALMAS_CLK_ID].platform_data = pdata->clk_pdata;
496 children[PALMAS_CLK_ID].pdata_size = sizeof(*pdata->clk_pdata);
498 ret = mfd_add_devices(palmas->dev, -1,
499 children, ARRAY_SIZE(palmas_children),
501 regmap_irq_get_domain(palmas->irq_data));
510 mfd_remove_devices(palmas->dev);
512 regmap_del_irq_chip(palmas->irq, palmas->irq_data);
517 static int palmas_i2c_remove(struct i2c_client *i2c)
519 struct palmas *palmas = i2c_get_clientdata(i2c);
521 mfd_remove_devices(palmas->dev);
522 regmap_del_irq_chip(palmas->irq, palmas->irq_data);
527 static const struct i2c_device_id palmas_i2c_id[] = {
534 MODULE_DEVICE_TABLE(i2c, palmas_i2c_id);
536 static struct of_device_id of_palmas_match_tbl[] = {
537 { .compatible = "ti,palmas", },
541 static struct i2c_driver palmas_i2c_driver = {
544 .of_match_table = of_palmas_match_tbl,
545 .owner = THIS_MODULE,
547 .probe = palmas_i2c_probe,
548 .remove = palmas_i2c_remove,
549 .id_table = palmas_i2c_id,
552 static int __init palmas_i2c_init(void)
554 return i2c_add_driver(&palmas_i2c_driver);
556 /* init early so consumer devices can complete system boot */
557 subsys_initcall(palmas_i2c_init);
559 static void __exit palmas_i2c_exit(void)
561 i2c_del_driver(&palmas_i2c_driver);
563 module_exit(palmas_i2c_exit);
565 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
566 MODULE_DESCRIPTION("Palmas chip family multi-function driver");
567 MODULE_LICENSE("GPL");