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 const struct mfd_cell palmas_children
[] = {
44 .name
= "palmas-pmic",
48 .name
= "palmas-gpio",
52 .name
= "palmas-leds",
64 .name
= "palmas-pwrbutton",
65 .id
= PALMAS_PWRBUTTON_ID
,
68 .name
= "palmas-gpadc",
69 .id
= PALMAS_GPADC_ID
,
72 .name
= "palmas-resource",
73 .id
= PALMAS_RESOURCE_ID
,
89 static const struct regmap_config palmas_regmap_config
[PALMAS_NUM_CLIENTS
] = {
93 .max_register
= PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE
,
94 PALMAS_PRIMARY_SECONDARY_PAD3
),
99 .max_register
= PALMAS_BASE_TO_REG(PALMAS_GPADC_BASE
,
100 PALMAS_GPADC_SMPS_VSEL_MONITORING
),
105 .max_register
= PALMAS_BASE_TO_REG(PALMAS_TRIM_GPADC_BASE
,
106 PALMAS_GPADC_TRIM16
),
110 static const struct regmap_irq palmas_irqs
[] = {
112 [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ
] = {
113 .mask
= PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV
,
115 [PALMAS_PWRON_IRQ
] = {
116 .mask
= PALMAS_INT1_STATUS_PWRON
,
118 [PALMAS_LONG_PRESS_KEY_IRQ
] = {
119 .mask
= PALMAS_INT1_STATUS_LONG_PRESS_KEY
,
121 [PALMAS_RPWRON_IRQ
] = {
122 .mask
= PALMAS_INT1_STATUS_RPWRON
,
124 [PALMAS_PWRDOWN_IRQ
] = {
125 .mask
= PALMAS_INT1_STATUS_PWRDOWN
,
127 [PALMAS_HOTDIE_IRQ
] = {
128 .mask
= PALMAS_INT1_STATUS_HOTDIE
,
130 [PALMAS_VSYS_MON_IRQ
] = {
131 .mask
= PALMAS_INT1_STATUS_VSYS_MON
,
133 [PALMAS_VBAT_MON_IRQ
] = {
134 .mask
= PALMAS_INT1_STATUS_VBAT_MON
,
137 [PALMAS_RTC_ALARM_IRQ
] = {
138 .mask
= PALMAS_INT2_STATUS_RTC_ALARM
,
141 [PALMAS_RTC_TIMER_IRQ
] = {
142 .mask
= PALMAS_INT2_STATUS_RTC_TIMER
,
146 .mask
= PALMAS_INT2_STATUS_WDT
,
149 [PALMAS_BATREMOVAL_IRQ
] = {
150 .mask
= PALMAS_INT2_STATUS_BATREMOVAL
,
153 [PALMAS_RESET_IN_IRQ
] = {
154 .mask
= PALMAS_INT2_STATUS_RESET_IN
,
157 [PALMAS_FBI_BB_IRQ
] = {
158 .mask
= PALMAS_INT2_STATUS_FBI_BB
,
161 [PALMAS_SHORT_IRQ
] = {
162 .mask
= PALMAS_INT2_STATUS_SHORT
,
165 [PALMAS_VAC_ACOK_IRQ
] = {
166 .mask
= PALMAS_INT2_STATUS_VAC_ACOK
,
170 [PALMAS_GPADC_AUTO_0_IRQ
] = {
171 .mask
= PALMAS_INT3_STATUS_GPADC_AUTO_0
,
174 [PALMAS_GPADC_AUTO_1_IRQ
] = {
175 .mask
= PALMAS_INT3_STATUS_GPADC_AUTO_1
,
178 [PALMAS_GPADC_EOC_SW_IRQ
] = {
179 .mask
= PALMAS_INT3_STATUS_GPADC_EOC_SW
,
182 [PALMAS_GPADC_EOC_RT_IRQ
] = {
183 .mask
= PALMAS_INT3_STATUS_GPADC_EOC_RT
,
186 [PALMAS_ID_OTG_IRQ
] = {
187 .mask
= PALMAS_INT3_STATUS_ID_OTG
,
191 .mask
= PALMAS_INT3_STATUS_ID
,
194 [PALMAS_VBUS_OTG_IRQ
] = {
195 .mask
= PALMAS_INT3_STATUS_VBUS_OTG
,
198 [PALMAS_VBUS_IRQ
] = {
199 .mask
= PALMAS_INT3_STATUS_VBUS
,
203 [PALMAS_GPIO_0_IRQ
] = {
204 .mask
= PALMAS_INT4_STATUS_GPIO_0
,
207 [PALMAS_GPIO_1_IRQ
] = {
208 .mask
= PALMAS_INT4_STATUS_GPIO_1
,
211 [PALMAS_GPIO_2_IRQ
] = {
212 .mask
= PALMAS_INT4_STATUS_GPIO_2
,
215 [PALMAS_GPIO_3_IRQ
] = {
216 .mask
= PALMAS_INT4_STATUS_GPIO_3
,
219 [PALMAS_GPIO_4_IRQ
] = {
220 .mask
= PALMAS_INT4_STATUS_GPIO_4
,
223 [PALMAS_GPIO_5_IRQ
] = {
224 .mask
= PALMAS_INT4_STATUS_GPIO_5
,
227 [PALMAS_GPIO_6_IRQ
] = {
228 .mask
= PALMAS_INT4_STATUS_GPIO_6
,
231 [PALMAS_GPIO_7_IRQ
] = {
232 .mask
= PALMAS_INT4_STATUS_GPIO_7
,
237 static struct regmap_irq_chip palmas_irq_chip
= {
240 .num_irqs
= ARRAY_SIZE(palmas_irqs
),
244 .status_base
= PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE
,
246 .mask_base
= PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE
,
250 static void palmas_dt_to_pdata(struct device_node
*node
,
251 struct palmas_platform_data
*pdata
)
256 ret
= of_property_read_u32(node
, "ti,mux_pad1", &prop
);
258 pdata
->mux_from_pdata
= 1;
262 ret
= of_property_read_u32(node
, "ti,mux_pad2", &prop
);
264 pdata
->mux_from_pdata
= 1;
268 /* The default for this register is all masked */
269 ret
= of_property_read_u32(node
, "ti,power_ctrl", &prop
);
271 pdata
->power_ctrl
= prop
;
273 pdata
->power_ctrl
= PALMAS_POWER_CTRL_NSLEEP_MASK
|
274 PALMAS_POWER_CTRL_ENABLE1_MASK
|
275 PALMAS_POWER_CTRL_ENABLE2_MASK
;
278 static int palmas_i2c_probe(struct i2c_client
*i2c
,
279 const struct i2c_device_id
*id
)
281 struct palmas
*palmas
;
282 struct palmas_platform_data
*pdata
;
283 struct device_node
*node
= i2c
->dev
.of_node
;
285 unsigned int reg
, addr
;
287 struct mfd_cell
*children
;
289 pdata
= dev_get_platdata(&i2c
->dev
);
291 if (node
&& !pdata
) {
292 pdata
= devm_kzalloc(&i2c
->dev
, sizeof(*pdata
), GFP_KERNEL
);
297 palmas_dt_to_pdata(node
, pdata
);
303 palmas
= devm_kzalloc(&i2c
->dev
, sizeof(struct palmas
), GFP_KERNEL
);
307 i2c_set_clientdata(i2c
, palmas
);
308 palmas
->dev
= &i2c
->dev
;
309 palmas
->id
= id
->driver_data
;
310 palmas
->irq
= i2c
->irq
;
312 for (i
= 0; i
< PALMAS_NUM_CLIENTS
; i
++) {
314 palmas
->i2c_clients
[i
] = i2c
;
316 palmas
->i2c_clients
[i
] =
317 i2c_new_dummy(i2c
->adapter
,
319 if (!palmas
->i2c_clients
[i
]) {
321 "can't attach client %d\n", i
);
326 palmas
->regmap
[i
] = devm_regmap_init_i2c(palmas
->i2c_clients
[i
],
327 &palmas_regmap_config
[i
]);
328 if (IS_ERR(palmas
->regmap
[i
])) {
329 ret
= PTR_ERR(palmas
->regmap
[i
]);
331 "Failed to allocate regmap %d, err: %d\n",
337 /* Change IRQ into clear on read mode for efficiency */
338 slave
= PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE
);
339 addr
= PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE
, PALMAS_INT_CTRL
);
340 reg
= PALMAS_INT_CTRL_INT_CLEAR
;
342 regmap_write(palmas
->regmap
[slave
], addr
, reg
);
344 ret
= regmap_add_irq_chip(palmas
->regmap
[slave
], palmas
->irq
,
345 IRQF_ONESHOT
| IRQF_TRIGGER_LOW
, 0, &palmas_irq_chip
,
350 slave
= PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE
);
351 addr
= PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE
,
352 PALMAS_PRIMARY_SECONDARY_PAD1
);
354 if (pdata
->mux_from_pdata
) {
356 ret
= regmap_write(palmas
->regmap
[slave
], addr
, reg
);
360 ret
= regmap_read(palmas
->regmap
[slave
], addr
, ®
);
365 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0
))
366 palmas
->gpio_muxed
|= PALMAS_GPIO_0_MUXED
;
367 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK
))
368 palmas
->gpio_muxed
|= PALMAS_GPIO_1_MUXED
;
369 else if ((reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK
) ==
370 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT
))
371 palmas
->led_muxed
|= PALMAS_LED1_MUXED
;
372 else if ((reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK
) ==
373 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT
))
374 palmas
->pwm_muxed
|= PALMAS_PWM1_MUXED
;
375 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK
))
376 palmas
->gpio_muxed
|= PALMAS_GPIO_2_MUXED
;
377 else if ((reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK
) ==
378 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT
))
379 palmas
->led_muxed
|= PALMAS_LED2_MUXED
;
380 else if ((reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK
) ==
381 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT
))
382 palmas
->pwm_muxed
|= PALMAS_PWM2_MUXED
;
383 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3
))
384 palmas
->gpio_muxed
|= PALMAS_GPIO_3_MUXED
;
386 addr
= PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE
,
387 PALMAS_PRIMARY_SECONDARY_PAD2
);
389 if (pdata
->mux_from_pdata
) {
391 ret
= regmap_write(palmas
->regmap
[slave
], addr
, reg
);
395 ret
= regmap_read(palmas
->regmap
[slave
], addr
, ®
);
400 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4
))
401 palmas
->gpio_muxed
|= PALMAS_GPIO_4_MUXED
;
402 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK
))
403 palmas
->gpio_muxed
|= PALMAS_GPIO_5_MUXED
;
404 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6
))
405 palmas
->gpio_muxed
|= PALMAS_GPIO_6_MUXED
;
406 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK
))
407 palmas
->gpio_muxed
|= PALMAS_GPIO_7_MUXED
;
409 dev_info(palmas
->dev
, "Muxing GPIO %x, PWM %x, LED %x\n",
410 palmas
->gpio_muxed
, palmas
->pwm_muxed
,
413 reg
= pdata
->power_ctrl
;
415 slave
= PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE
);
416 addr
= PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE
, PALMAS_POWER_CTRL
);
418 ret
= regmap_write(palmas
->regmap
[slave
], addr
, reg
);
423 * If we are probing with DT do this the DT way and return here
424 * otherwise continue and add devices using mfd helpers.
427 ret
= of_platform_populate(node
, NULL
, NULL
, &i2c
->dev
);
434 children
= kmemdup(palmas_children
, sizeof(palmas_children
),
441 children
[PALMAS_PMIC_ID
].platform_data
= pdata
->pmic_pdata
;
442 children
[PALMAS_PMIC_ID
].pdata_size
= sizeof(*pdata
->pmic_pdata
);
444 children
[PALMAS_GPADC_ID
].platform_data
= pdata
->gpadc_pdata
;
445 children
[PALMAS_GPADC_ID
].pdata_size
= sizeof(*pdata
->gpadc_pdata
);
447 children
[PALMAS_RESOURCE_ID
].platform_data
= pdata
->resource_pdata
;
448 children
[PALMAS_RESOURCE_ID
].pdata_size
=
449 sizeof(*pdata
->resource_pdata
);
451 children
[PALMAS_USB_ID
].platform_data
= pdata
->usb_pdata
;
452 children
[PALMAS_USB_ID
].pdata_size
= sizeof(*pdata
->usb_pdata
);
454 children
[PALMAS_CLK_ID
].platform_data
= pdata
->clk_pdata
;
455 children
[PALMAS_CLK_ID
].pdata_size
= sizeof(*pdata
->clk_pdata
);
457 ret
= mfd_add_devices(palmas
->dev
, -1,
458 children
, ARRAY_SIZE(palmas_children
),
459 NULL
, regmap_irq_chip_get_base(palmas
->irq_data
),
469 mfd_remove_devices(palmas
->dev
);
471 regmap_del_irq_chip(palmas
->irq
, palmas
->irq_data
);
476 static int palmas_i2c_remove(struct i2c_client
*i2c
)
478 struct palmas
*palmas
= i2c_get_clientdata(i2c
);
480 mfd_remove_devices(palmas
->dev
);
481 regmap_del_irq_chip(palmas
->irq
, palmas
->irq_data
);
486 static const struct i2c_device_id palmas_i2c_id
[] = {
493 MODULE_DEVICE_TABLE(i2c
, palmas_i2c_id
);
495 static struct of_device_id of_palmas_match_tbl
[] = {
496 { .compatible
= "ti,palmas", },
500 static struct i2c_driver palmas_i2c_driver
= {
503 .of_match_table
= of_palmas_match_tbl
,
504 .owner
= THIS_MODULE
,
506 .probe
= palmas_i2c_probe
,
507 .remove
= palmas_i2c_remove
,
508 .id_table
= palmas_i2c_id
,
511 static int __init
palmas_i2c_init(void)
513 return i2c_add_driver(&palmas_i2c_driver
);
515 /* init early so consumer devices can complete system boot */
516 subsys_initcall(palmas_i2c_init
);
518 static void __exit
palmas_i2c_exit(void)
520 i2c_del_driver(&palmas_i2c_driver
);
522 module_exit(palmas_i2c_exit
);
524 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
525 MODULE_DESCRIPTION("Palmas chip family multi-function driver");
526 MODULE_LICENSE("GPL");