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_device.h>
28 #define PALMAS_EXT_REQ (PALMAS_EXT_CONTROL_ENABLE1 | \
29 PALMAS_EXT_CONTROL_ENABLE2 | \
30 PALMAS_EXT_CONTROL_NSLEEP)
32 struct palmas_sleep_requestor_info
{
38 #define EXTERNAL_REQUESTOR(_id, _offset, _pos) \
39 [PALMAS_EXTERNAL_REQSTR_ID_##_id] = { \
40 .id = PALMAS_EXTERNAL_REQSTR_ID_##_id, \
41 .reg_offset = _offset, \
45 static struct palmas_sleep_requestor_info sleep_req_info
[] = {
46 EXTERNAL_REQUESTOR(REGEN1
, 0, 0),
47 EXTERNAL_REQUESTOR(REGEN2
, 0, 1),
48 EXTERNAL_REQUESTOR(SYSEN1
, 0, 2),
49 EXTERNAL_REQUESTOR(SYSEN2
, 0, 3),
50 EXTERNAL_REQUESTOR(CLK32KG
, 0, 4),
51 EXTERNAL_REQUESTOR(CLK32KGAUDIO
, 0, 5),
52 EXTERNAL_REQUESTOR(REGEN3
, 0, 6),
53 EXTERNAL_REQUESTOR(SMPS12
, 1, 0),
54 EXTERNAL_REQUESTOR(SMPS3
, 1, 1),
55 EXTERNAL_REQUESTOR(SMPS45
, 1, 2),
56 EXTERNAL_REQUESTOR(SMPS6
, 1, 3),
57 EXTERNAL_REQUESTOR(SMPS7
, 1, 4),
58 EXTERNAL_REQUESTOR(SMPS8
, 1, 5),
59 EXTERNAL_REQUESTOR(SMPS9
, 1, 6),
60 EXTERNAL_REQUESTOR(SMPS10
, 1, 7),
61 EXTERNAL_REQUESTOR(LDO1
, 2, 0),
62 EXTERNAL_REQUESTOR(LDO2
, 2, 1),
63 EXTERNAL_REQUESTOR(LDO3
, 2, 2),
64 EXTERNAL_REQUESTOR(LDO4
, 2, 3),
65 EXTERNAL_REQUESTOR(LDO5
, 2, 4),
66 EXTERNAL_REQUESTOR(LDO6
, 2, 5),
67 EXTERNAL_REQUESTOR(LDO7
, 2, 6),
68 EXTERNAL_REQUESTOR(LDO8
, 2, 7),
69 EXTERNAL_REQUESTOR(LDO9
, 3, 0),
70 EXTERNAL_REQUESTOR(LDOLN
, 3, 1),
71 EXTERNAL_REQUESTOR(LDOUSB
, 3, 2),
74 static const struct regmap_config palmas_regmap_config
[PALMAS_NUM_CLIENTS
] = {
78 .max_register
= PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE
,
79 PALMAS_PRIMARY_SECONDARY_PAD3
),
84 .max_register
= PALMAS_BASE_TO_REG(PALMAS_GPADC_BASE
,
85 PALMAS_GPADC_SMPS_VSEL_MONITORING
),
90 .max_register
= PALMAS_BASE_TO_REG(PALMAS_TRIM_GPADC_BASE
,
95 static const struct regmap_irq palmas_irqs
[] = {
97 [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ
] = {
98 .mask
= PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV
,
100 [PALMAS_PWRON_IRQ
] = {
101 .mask
= PALMAS_INT1_STATUS_PWRON
,
103 [PALMAS_LONG_PRESS_KEY_IRQ
] = {
104 .mask
= PALMAS_INT1_STATUS_LONG_PRESS_KEY
,
106 [PALMAS_RPWRON_IRQ
] = {
107 .mask
= PALMAS_INT1_STATUS_RPWRON
,
109 [PALMAS_PWRDOWN_IRQ
] = {
110 .mask
= PALMAS_INT1_STATUS_PWRDOWN
,
112 [PALMAS_HOTDIE_IRQ
] = {
113 .mask
= PALMAS_INT1_STATUS_HOTDIE
,
115 [PALMAS_VSYS_MON_IRQ
] = {
116 .mask
= PALMAS_INT1_STATUS_VSYS_MON
,
118 [PALMAS_VBAT_MON_IRQ
] = {
119 .mask
= PALMAS_INT1_STATUS_VBAT_MON
,
122 [PALMAS_RTC_ALARM_IRQ
] = {
123 .mask
= PALMAS_INT2_STATUS_RTC_ALARM
,
126 [PALMAS_RTC_TIMER_IRQ
] = {
127 .mask
= PALMAS_INT2_STATUS_RTC_TIMER
,
131 .mask
= PALMAS_INT2_STATUS_WDT
,
134 [PALMAS_BATREMOVAL_IRQ
] = {
135 .mask
= PALMAS_INT2_STATUS_BATREMOVAL
,
138 [PALMAS_RESET_IN_IRQ
] = {
139 .mask
= PALMAS_INT2_STATUS_RESET_IN
,
142 [PALMAS_FBI_BB_IRQ
] = {
143 .mask
= PALMAS_INT2_STATUS_FBI_BB
,
146 [PALMAS_SHORT_IRQ
] = {
147 .mask
= PALMAS_INT2_STATUS_SHORT
,
150 [PALMAS_VAC_ACOK_IRQ
] = {
151 .mask
= PALMAS_INT2_STATUS_VAC_ACOK
,
155 [PALMAS_GPADC_AUTO_0_IRQ
] = {
156 .mask
= PALMAS_INT3_STATUS_GPADC_AUTO_0
,
159 [PALMAS_GPADC_AUTO_1_IRQ
] = {
160 .mask
= PALMAS_INT3_STATUS_GPADC_AUTO_1
,
163 [PALMAS_GPADC_EOC_SW_IRQ
] = {
164 .mask
= PALMAS_INT3_STATUS_GPADC_EOC_SW
,
167 [PALMAS_GPADC_EOC_RT_IRQ
] = {
168 .mask
= PALMAS_INT3_STATUS_GPADC_EOC_RT
,
171 [PALMAS_ID_OTG_IRQ
] = {
172 .mask
= PALMAS_INT3_STATUS_ID_OTG
,
176 .mask
= PALMAS_INT3_STATUS_ID
,
179 [PALMAS_VBUS_OTG_IRQ
] = {
180 .mask
= PALMAS_INT3_STATUS_VBUS_OTG
,
183 [PALMAS_VBUS_IRQ
] = {
184 .mask
= PALMAS_INT3_STATUS_VBUS
,
188 [PALMAS_GPIO_0_IRQ
] = {
189 .mask
= PALMAS_INT4_STATUS_GPIO_0
,
192 [PALMAS_GPIO_1_IRQ
] = {
193 .mask
= PALMAS_INT4_STATUS_GPIO_1
,
196 [PALMAS_GPIO_2_IRQ
] = {
197 .mask
= PALMAS_INT4_STATUS_GPIO_2
,
200 [PALMAS_GPIO_3_IRQ
] = {
201 .mask
= PALMAS_INT4_STATUS_GPIO_3
,
204 [PALMAS_GPIO_4_IRQ
] = {
205 .mask
= PALMAS_INT4_STATUS_GPIO_4
,
208 [PALMAS_GPIO_5_IRQ
] = {
209 .mask
= PALMAS_INT4_STATUS_GPIO_5
,
212 [PALMAS_GPIO_6_IRQ
] = {
213 .mask
= PALMAS_INT4_STATUS_GPIO_6
,
216 [PALMAS_GPIO_7_IRQ
] = {
217 .mask
= PALMAS_INT4_STATUS_GPIO_7
,
222 static struct regmap_irq_chip palmas_irq_chip
= {
225 .num_irqs
= ARRAY_SIZE(palmas_irqs
),
229 .status_base
= PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE
,
231 .mask_base
= PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE
,
235 int palmas_ext_control_req_config(struct palmas
*palmas
,
236 enum palmas_external_requestor_id id
, int ext_ctrl
, bool enable
)
238 int preq_mask_bit
= 0;
243 if (!(ext_ctrl
& PALMAS_EXT_REQ
))
246 if (id
>= PALMAS_EXTERNAL_REQSTR_ID_MAX
)
249 if (ext_ctrl
& PALMAS_EXT_CONTROL_NSLEEP
) {
250 reg_add
= PALMAS_NSLEEP_RES_ASSIGN
;
252 } else if (ext_ctrl
& PALMAS_EXT_CONTROL_ENABLE1
) {
253 reg_add
= PALMAS_ENABLE1_RES_ASSIGN
;
255 } else if (ext_ctrl
& PALMAS_EXT_CONTROL_ENABLE2
) {
256 reg_add
= PALMAS_ENABLE2_RES_ASSIGN
;
260 bit_pos
= sleep_req_info
[id
].bit_pos
;
261 reg_add
+= sleep_req_info
[id
].reg_offset
;
263 ret
= palmas_update_bits(palmas
, PALMAS_RESOURCE_BASE
,
264 reg_add
, BIT(bit_pos
), BIT(bit_pos
));
266 ret
= palmas_update_bits(palmas
, PALMAS_RESOURCE_BASE
,
267 reg_add
, BIT(bit_pos
), 0);
269 dev_err(palmas
->dev
, "Resource reg 0x%02x update failed %d\n",
274 /* Unmask the PREQ */
275 ret
= palmas_update_bits(palmas
, PALMAS_PMU_CONTROL_BASE
,
276 PALMAS_POWER_CTRL
, BIT(preq_mask_bit
), 0);
278 dev_err(palmas
->dev
, "POWER_CTRL register update failed %d\n",
284 EXPORT_SYMBOL_GPL(palmas_ext_control_req_config
);
286 static int palmas_set_pdata_irq_flag(struct i2c_client
*i2c
,
287 struct palmas_platform_data
*pdata
)
289 struct irq_data
*irq_data
= irq_get_irq_data(i2c
->irq
);
291 dev_err(&i2c
->dev
, "Invalid IRQ: %d\n", i2c
->irq
);
295 pdata
->irq_flags
= irqd_get_trigger_type(irq_data
);
296 dev_info(&i2c
->dev
, "Irq flag is 0x%08x\n", pdata
->irq_flags
);
300 static void palmas_dt_to_pdata(struct i2c_client
*i2c
,
301 struct palmas_platform_data
*pdata
)
303 struct device_node
*node
= i2c
->dev
.of_node
;
307 ret
= of_property_read_u32(node
, "ti,mux-pad1", &prop
);
309 pdata
->mux_from_pdata
= 1;
313 ret
= of_property_read_u32(node
, "ti,mux-pad2", &prop
);
315 pdata
->mux_from_pdata
= 1;
319 /* The default for this register is all masked */
320 ret
= of_property_read_u32(node
, "ti,power-ctrl", &prop
);
322 pdata
->power_ctrl
= prop
;
324 pdata
->power_ctrl
= PALMAS_POWER_CTRL_NSLEEP_MASK
|
325 PALMAS_POWER_CTRL_ENABLE1_MASK
|
326 PALMAS_POWER_CTRL_ENABLE2_MASK
;
328 palmas_set_pdata_irq_flag(i2c
, pdata
);
330 pdata
->pm_off
= of_property_read_bool(node
,
331 "ti,system-power-controller");
334 static struct palmas
*palmas_dev
;
335 static void palmas_power_off(void)
343 slave
= PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE
);
344 addr
= PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE
, PALMAS_DEV_CTRL
);
346 ret
= regmap_update_bits(
347 palmas_dev
->regmap
[slave
],
349 PALMAS_DEV_CTRL_DEV_ON
,
353 pr_err("%s: Unable to write to DEV_CTRL_DEV_ON: %d\n",
357 static unsigned int palmas_features
= PALMAS_PMIC_FEATURE_SMPS10_BOOST
;
358 static unsigned int tps659038_features
;
360 static const struct of_device_id of_palmas_match_tbl
[] = {
362 .compatible
= "ti,palmas",
363 .data
= &palmas_features
,
366 .compatible
= "ti,tps659038",
367 .data
= &tps659038_features
,
371 MODULE_DEVICE_TABLE(of
, of_palmas_match_tbl
);
373 static int palmas_i2c_probe(struct i2c_client
*i2c
,
374 const struct i2c_device_id
*id
)
376 struct palmas
*palmas
;
377 struct palmas_platform_data
*pdata
;
378 struct device_node
*node
= i2c
->dev
.of_node
;
380 unsigned int reg
, addr
, *features
;
382 const struct of_device_id
*match
;
384 pdata
= dev_get_platdata(&i2c
->dev
);
386 if (node
&& !pdata
) {
387 pdata
= devm_kzalloc(&i2c
->dev
, sizeof(*pdata
), GFP_KERNEL
);
392 palmas_dt_to_pdata(i2c
, pdata
);
398 palmas
= devm_kzalloc(&i2c
->dev
, sizeof(struct palmas
), GFP_KERNEL
);
402 i2c_set_clientdata(i2c
, palmas
);
403 palmas
->dev
= &i2c
->dev
;
404 palmas
->irq
= i2c
->irq
;
406 match
= of_match_device(of_palmas_match_tbl
, &i2c
->dev
);
411 features
= (unsigned int *)match
->data
;
412 palmas
->features
= *features
;
414 for (i
= 0; i
< PALMAS_NUM_CLIENTS
; i
++) {
416 palmas
->i2c_clients
[i
] = i2c
;
418 palmas
->i2c_clients
[i
] =
419 i2c_new_dummy(i2c
->adapter
,
421 if (!palmas
->i2c_clients
[i
]) {
423 "can't attach client %d\n", i
);
427 palmas
->i2c_clients
[i
]->dev
.of_node
= of_node_get(node
);
429 palmas
->regmap
[i
] = devm_regmap_init_i2c(palmas
->i2c_clients
[i
],
430 &palmas_regmap_config
[i
]);
431 if (IS_ERR(palmas
->regmap
[i
])) {
432 ret
= PTR_ERR(palmas
->regmap
[i
]);
434 "Failed to allocate regmap %d, err: %d\n",
441 dev_warn(palmas
->dev
, "IRQ missing: skipping irq request\n");
445 /* Change interrupt line output polarity */
446 if (pdata
->irq_flags
& IRQ_TYPE_LEVEL_HIGH
)
447 reg
= PALMAS_POLARITY_CTRL_INT_POLARITY
;
450 ret
= palmas_update_bits(palmas
, PALMAS_PU_PD_OD_BASE
,
451 PALMAS_POLARITY_CTRL
, PALMAS_POLARITY_CTRL_INT_POLARITY
,
454 dev_err(palmas
->dev
, "POLARITY_CTRL updat failed: %d\n", ret
);
458 /* Change IRQ into clear on read mode for efficiency */
459 slave
= PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE
);
460 addr
= PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE
, PALMAS_INT_CTRL
);
461 reg
= PALMAS_INT_CTRL_INT_CLEAR
;
463 regmap_write(palmas
->regmap
[slave
], addr
, reg
);
465 ret
= regmap_add_irq_chip(palmas
->regmap
[slave
], palmas
->irq
,
466 IRQF_ONESHOT
| pdata
->irq_flags
, 0, &palmas_irq_chip
,
472 slave
= PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE
);
473 addr
= PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE
,
474 PALMAS_PRIMARY_SECONDARY_PAD1
);
476 if (pdata
->mux_from_pdata
) {
478 ret
= regmap_write(palmas
->regmap
[slave
], addr
, reg
);
482 ret
= regmap_read(palmas
->regmap
[slave
], addr
, ®
);
487 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0
))
488 palmas
->gpio_muxed
|= PALMAS_GPIO_0_MUXED
;
489 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK
))
490 palmas
->gpio_muxed
|= PALMAS_GPIO_1_MUXED
;
491 else if ((reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK
) ==
492 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT
))
493 palmas
->led_muxed
|= PALMAS_LED1_MUXED
;
494 else if ((reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK
) ==
495 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT
))
496 palmas
->pwm_muxed
|= PALMAS_PWM1_MUXED
;
497 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK
))
498 palmas
->gpio_muxed
|= PALMAS_GPIO_2_MUXED
;
499 else if ((reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK
) ==
500 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT
))
501 palmas
->led_muxed
|= PALMAS_LED2_MUXED
;
502 else if ((reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK
) ==
503 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT
))
504 palmas
->pwm_muxed
|= PALMAS_PWM2_MUXED
;
505 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3
))
506 palmas
->gpio_muxed
|= PALMAS_GPIO_3_MUXED
;
508 addr
= PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE
,
509 PALMAS_PRIMARY_SECONDARY_PAD2
);
511 if (pdata
->mux_from_pdata
) {
513 ret
= regmap_write(palmas
->regmap
[slave
], addr
, reg
);
517 ret
= regmap_read(palmas
->regmap
[slave
], addr
, ®
);
522 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4
))
523 palmas
->gpio_muxed
|= PALMAS_GPIO_4_MUXED
;
524 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK
))
525 palmas
->gpio_muxed
|= PALMAS_GPIO_5_MUXED
;
526 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6
))
527 palmas
->gpio_muxed
|= PALMAS_GPIO_6_MUXED
;
528 if (!(reg
& PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK
))
529 palmas
->gpio_muxed
|= PALMAS_GPIO_7_MUXED
;
531 dev_info(palmas
->dev
, "Muxing GPIO %x, PWM %x, LED %x\n",
532 palmas
->gpio_muxed
, palmas
->pwm_muxed
,
535 reg
= pdata
->power_ctrl
;
537 slave
= PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE
);
538 addr
= PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE
, PALMAS_POWER_CTRL
);
540 ret
= regmap_write(palmas
->regmap
[slave
], addr
, reg
);
545 * If we are probing with DT do this the DT way and return here
546 * otherwise continue and add devices using mfd helpers.
549 ret
= of_platform_populate(node
, NULL
, NULL
, &i2c
->dev
);
552 } else if (pdata
->pm_off
&& !pm_power_off
) {
554 pm_power_off
= palmas_power_off
;
561 regmap_del_irq_chip(palmas
->irq
, palmas
->irq_data
);
563 for (i
= 1; i
< PALMAS_NUM_CLIENTS
; i
++) {
564 if (palmas
->i2c_clients
[i
])
565 i2c_unregister_device(palmas
->i2c_clients
[i
]);
570 static int palmas_i2c_remove(struct i2c_client
*i2c
)
572 struct palmas
*palmas
= i2c_get_clientdata(i2c
);
575 regmap_del_irq_chip(palmas
->irq
, palmas
->irq_data
);
577 for (i
= 1; i
< PALMAS_NUM_CLIENTS
; i
++) {
578 if (palmas
->i2c_clients
[i
])
579 i2c_unregister_device(palmas
->i2c_clients
[i
]);
582 if (palmas
== palmas_dev
) {
590 static const struct i2c_device_id palmas_i2c_id
[] = {
597 MODULE_DEVICE_TABLE(i2c
, palmas_i2c_id
);
599 static struct i2c_driver palmas_i2c_driver
= {
602 .of_match_table
= of_palmas_match_tbl
,
603 .owner
= THIS_MODULE
,
605 .probe
= palmas_i2c_probe
,
606 .remove
= palmas_i2c_remove
,
607 .id_table
= palmas_i2c_id
,
610 static int __init
palmas_i2c_init(void)
612 return i2c_add_driver(&palmas_i2c_driver
);
614 /* init early so consumer devices can complete system boot */
615 subsys_initcall(palmas_i2c_init
);
617 static void __exit
palmas_i2c_exit(void)
619 i2c_del_driver(&palmas_i2c_driver
);
621 module_exit(palmas_i2c_exit
);
623 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
624 MODULE_DESCRIPTION("Palmas chip family multi-function driver");
625 MODULE_LICENSE("GPL");