blob: a4e53caa76cd27f2c5f5b689e3d653df91818c05 [file] [log] [blame]
Graeme Gregory2945fbc2012-05-15 15:48:56 +09001/*
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>
J Keerthy1ffb0be2013-06-19 11:27:48 +053026#include <linux/of_device.h>
Graeme Gregory2945fbc2012-05-15 15:48:56 +090027
Graeme Gregory2945fbc2012-05-15 15:48:56 +090028static const struct regmap_config palmas_regmap_config[PALMAS_NUM_CLIENTS] = {
29 {
30 .reg_bits = 8,
31 .val_bits = 8,
32 .max_register = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
33 PALMAS_PRIMARY_SECONDARY_PAD3),
34 },
35 {
36 .reg_bits = 8,
37 .val_bits = 8,
38 .max_register = PALMAS_BASE_TO_REG(PALMAS_GPADC_BASE,
39 PALMAS_GPADC_SMPS_VSEL_MONITORING),
40 },
41 {
42 .reg_bits = 8,
43 .val_bits = 8,
44 .max_register = PALMAS_BASE_TO_REG(PALMAS_TRIM_GPADC_BASE,
45 PALMAS_GPADC_TRIM16),
46 },
47};
48
49static const struct regmap_irq palmas_irqs[] = {
50 /* INT1 IRQs */
51 [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ] = {
52 .mask = PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV,
53 },
54 [PALMAS_PWRON_IRQ] = {
55 .mask = PALMAS_INT1_STATUS_PWRON,
56 },
57 [PALMAS_LONG_PRESS_KEY_IRQ] = {
58 .mask = PALMAS_INT1_STATUS_LONG_PRESS_KEY,
59 },
60 [PALMAS_RPWRON_IRQ] = {
61 .mask = PALMAS_INT1_STATUS_RPWRON,
62 },
63 [PALMAS_PWRDOWN_IRQ] = {
64 .mask = PALMAS_INT1_STATUS_PWRDOWN,
65 },
66 [PALMAS_HOTDIE_IRQ] = {
67 .mask = PALMAS_INT1_STATUS_HOTDIE,
68 },
69 [PALMAS_VSYS_MON_IRQ] = {
70 .mask = PALMAS_INT1_STATUS_VSYS_MON,
71 },
72 [PALMAS_VBAT_MON_IRQ] = {
73 .mask = PALMAS_INT1_STATUS_VBAT_MON,
74 },
75 /* INT2 IRQs*/
76 [PALMAS_RTC_ALARM_IRQ] = {
77 .mask = PALMAS_INT2_STATUS_RTC_ALARM,
78 .reg_offset = 1,
79 },
80 [PALMAS_RTC_TIMER_IRQ] = {
81 .mask = PALMAS_INT2_STATUS_RTC_TIMER,
82 .reg_offset = 1,
83 },
84 [PALMAS_WDT_IRQ] = {
85 .mask = PALMAS_INT2_STATUS_WDT,
86 .reg_offset = 1,
87 },
88 [PALMAS_BATREMOVAL_IRQ] = {
89 .mask = PALMAS_INT2_STATUS_BATREMOVAL,
90 .reg_offset = 1,
91 },
92 [PALMAS_RESET_IN_IRQ] = {
93 .mask = PALMAS_INT2_STATUS_RESET_IN,
94 .reg_offset = 1,
95 },
96 [PALMAS_FBI_BB_IRQ] = {
97 .mask = PALMAS_INT2_STATUS_FBI_BB,
98 .reg_offset = 1,
99 },
100 [PALMAS_SHORT_IRQ] = {
101 .mask = PALMAS_INT2_STATUS_SHORT,
102 .reg_offset = 1,
103 },
104 [PALMAS_VAC_ACOK_IRQ] = {
105 .mask = PALMAS_INT2_STATUS_VAC_ACOK,
106 .reg_offset = 1,
107 },
108 /* INT3 IRQs */
109 [PALMAS_GPADC_AUTO_0_IRQ] = {
110 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_0,
111 .reg_offset = 2,
112 },
113 [PALMAS_GPADC_AUTO_1_IRQ] = {
114 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_1,
115 .reg_offset = 2,
116 },
117 [PALMAS_GPADC_EOC_SW_IRQ] = {
118 .mask = PALMAS_INT3_STATUS_GPADC_EOC_SW,
119 .reg_offset = 2,
120 },
121 [PALMAS_GPADC_EOC_RT_IRQ] = {
122 .mask = PALMAS_INT3_STATUS_GPADC_EOC_RT,
123 .reg_offset = 2,
124 },
125 [PALMAS_ID_OTG_IRQ] = {
126 .mask = PALMAS_INT3_STATUS_ID_OTG,
127 .reg_offset = 2,
128 },
129 [PALMAS_ID_IRQ] = {
130 .mask = PALMAS_INT3_STATUS_ID,
131 .reg_offset = 2,
132 },
133 [PALMAS_VBUS_OTG_IRQ] = {
134 .mask = PALMAS_INT3_STATUS_VBUS_OTG,
135 .reg_offset = 2,
136 },
137 [PALMAS_VBUS_IRQ] = {
138 .mask = PALMAS_INT3_STATUS_VBUS,
139 .reg_offset = 2,
140 },
141 /* INT4 IRQs */
142 [PALMAS_GPIO_0_IRQ] = {
143 .mask = PALMAS_INT4_STATUS_GPIO_0,
144 .reg_offset = 3,
145 },
146 [PALMAS_GPIO_1_IRQ] = {
147 .mask = PALMAS_INT4_STATUS_GPIO_1,
148 .reg_offset = 3,
149 },
150 [PALMAS_GPIO_2_IRQ] = {
151 .mask = PALMAS_INT4_STATUS_GPIO_2,
152 .reg_offset = 3,
153 },
154 [PALMAS_GPIO_3_IRQ] = {
155 .mask = PALMAS_INT4_STATUS_GPIO_3,
156 .reg_offset = 3,
157 },
158 [PALMAS_GPIO_4_IRQ] = {
159 .mask = PALMAS_INT4_STATUS_GPIO_4,
160 .reg_offset = 3,
161 },
162 [PALMAS_GPIO_5_IRQ] = {
163 .mask = PALMAS_INT4_STATUS_GPIO_5,
164 .reg_offset = 3,
165 },
166 [PALMAS_GPIO_6_IRQ] = {
167 .mask = PALMAS_INT4_STATUS_GPIO_6,
168 .reg_offset = 3,
169 },
170 [PALMAS_GPIO_7_IRQ] = {
171 .mask = PALMAS_INT4_STATUS_GPIO_7,
172 .reg_offset = 3,
173 },
174};
175
176static struct regmap_irq_chip palmas_irq_chip = {
177 .name = "palmas",
178 .irqs = palmas_irqs,
179 .num_irqs = ARRAY_SIZE(palmas_irqs),
180
181 .num_regs = 4,
182 .irq_reg_stride = 5,
183 .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
184 PALMAS_INT1_STATUS),
185 .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
186 PALMAS_INT1_MASK),
187};
188
Laxman Dewangandf545d12013-03-01 20:13:46 +0530189static int palmas_set_pdata_irq_flag(struct i2c_client *i2c,
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200190 struct palmas_platform_data *pdata)
191{
Laxman Dewangandf545d12013-03-01 20:13:46 +0530192 struct irq_data *irq_data = irq_get_irq_data(i2c->irq);
193 if (!irq_data) {
194 dev_err(&i2c->dev, "Invalid IRQ: %d\n", i2c->irq);
195 return -EINVAL;
196 }
197
198 pdata->irq_flags = irqd_get_trigger_type(irq_data);
199 dev_info(&i2c->dev, "Irq flag is 0x%08x\n", pdata->irq_flags);
200 return 0;
201}
202
203static void palmas_dt_to_pdata(struct i2c_client *i2c,
204 struct palmas_platform_data *pdata)
205{
206 struct device_node *node = i2c->dev.of_node;
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200207 int ret;
208 u32 prop;
209
J Keerthy2154a2b2013-02-18 10:42:44 +0530210 ret = of_property_read_u32(node, "ti,mux-pad1", &prop);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200211 if (!ret) {
212 pdata->mux_from_pdata = 1;
213 pdata->pad1 = prop;
214 }
215
J Keerthy2154a2b2013-02-18 10:42:44 +0530216 ret = of_property_read_u32(node, "ti,mux-pad2", &prop);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200217 if (!ret) {
218 pdata->mux_from_pdata = 1;
219 pdata->pad2 = prop;
220 }
221
222 /* The default for this register is all masked */
J Keerthy2154a2b2013-02-18 10:42:44 +0530223 ret = of_property_read_u32(node, "ti,power-ctrl", &prop);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200224 if (!ret)
225 pdata->power_ctrl = prop;
226 else
227 pdata->power_ctrl = PALMAS_POWER_CTRL_NSLEEP_MASK |
228 PALMAS_POWER_CTRL_ENABLE1_MASK |
229 PALMAS_POWER_CTRL_ENABLE2_MASK;
Laxman Dewangandf545d12013-03-01 20:13:46 +0530230 if (i2c->irq)
231 palmas_set_pdata_irq_flag(i2c, pdata);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200232}
233
J Keerthy1ffb0be2013-06-19 11:27:48 +0530234static unsigned int palmas_features = PALMAS_PMIC_FEATURE_SMPS10_BOOST;
235
236static const struct of_device_id of_palmas_match_tbl[] = {
237 {
238 .compatible = "ti,palmas",
239 .data = &palmas_features,
240 },
241 { },
242};
243
Bill Pembertonf791be42012-11-19 13:23:04 -0500244static int palmas_i2c_probe(struct i2c_client *i2c,
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900245 const struct i2c_device_id *id)
246{
247 struct palmas *palmas;
248 struct palmas_platform_data *pdata;
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200249 struct device_node *node = i2c->dev.of_node;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900250 int ret = 0, i;
J Keerthy1ffb0be2013-06-19 11:27:48 +0530251 unsigned int reg, addr, *features;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900252 int slave;
J Keerthy1ffb0be2013-06-19 11:27:48 +0530253 const struct of_device_id *match;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900254
255 pdata = dev_get_platdata(&i2c->dev);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200256
257 if (node && !pdata) {
258 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
259
260 if (!pdata)
261 return -ENOMEM;
262
Laxman Dewangandf545d12013-03-01 20:13:46 +0530263 palmas_dt_to_pdata(i2c, pdata);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200264 }
265
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900266 if (!pdata)
267 return -EINVAL;
268
269 palmas = devm_kzalloc(&i2c->dev, sizeof(struct palmas), GFP_KERNEL);
270 if (palmas == NULL)
271 return -ENOMEM;
272
273 i2c_set_clientdata(i2c, palmas);
274 palmas->dev = &i2c->dev;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900275 palmas->irq = i2c->irq;
276
J Keerthy1ffb0be2013-06-19 11:27:48 +0530277 match = of_match_device(of_match_ptr(of_palmas_match_tbl), &i2c->dev);
278
279 if (!match)
280 return -ENODATA;
281
282 features = (unsigned int *)match->data;
283 palmas->features = *features;
284
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900285 for (i = 0; i < PALMAS_NUM_CLIENTS; i++) {
286 if (i == 0)
287 palmas->i2c_clients[i] = i2c;
288 else {
289 palmas->i2c_clients[i] =
290 i2c_new_dummy(i2c->adapter,
291 i2c->addr + i);
292 if (!palmas->i2c_clients[i]) {
293 dev_err(palmas->dev,
294 "can't attach client %d\n", i);
295 ret = -ENOMEM;
296 goto err;
297 }
Laxman Dewanganc4fbec32013-03-19 14:28:20 +0530298 palmas->i2c_clients[i]->dev.of_node = of_node_get(node);
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900299 }
300 palmas->regmap[i] = devm_regmap_init_i2c(palmas->i2c_clients[i],
301 &palmas_regmap_config[i]);
302 if (IS_ERR(palmas->regmap[i])) {
303 ret = PTR_ERR(palmas->regmap[i]);
304 dev_err(palmas->dev,
305 "Failed to allocate regmap %d, err: %d\n",
306 i, ret);
307 goto err;
308 }
309 }
310
J Keerthyad522f42013-06-19 11:27:47 +0530311 if (!palmas->irq) {
312 dev_warn(palmas->dev, "IRQ missing: skipping irq request\n");
313 goto no_irq;
314 }
315
Laxman Dewangandf545d12013-03-01 20:13:46 +0530316 /* Change interrupt line output polarity */
317 if (pdata->irq_flags & IRQ_TYPE_LEVEL_HIGH)
318 reg = PALMAS_POLARITY_CTRL_INT_POLARITY;
319 else
320 reg = 0;
321 ret = palmas_update_bits(palmas, PALMAS_PU_PD_OD_BASE,
322 PALMAS_POLARITY_CTRL, PALMAS_POLARITY_CTRL_INT_POLARITY,
323 reg);
324 if (ret < 0) {
325 dev_err(palmas->dev, "POLARITY_CTRL updat failed: %d\n", ret);
326 goto err;
327 }
328
Graeme Gregoryb330f852012-06-22 13:36:19 +0100329 /* Change IRQ into clear on read mode for efficiency */
330 slave = PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE);
331 addr = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, PALMAS_INT_CTRL);
332 reg = PALMAS_INT_CTRL_INT_CLEAR;
333
334 regmap_write(palmas->regmap[slave], addr, reg);
335
336 ret = regmap_add_irq_chip(palmas->regmap[slave], palmas->irq,
Laxman Dewangandf545d12013-03-01 20:13:46 +0530337 IRQF_ONESHOT | pdata->irq_flags, 0, &palmas_irq_chip,
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900338 &palmas->irq_data);
339 if (ret < 0)
340 goto err;
341
J Keerthyad522f42013-06-19 11:27:47 +0530342no_irq:
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900343 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
344 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
345 PALMAS_PRIMARY_SECONDARY_PAD1);
346
347 if (pdata->mux_from_pdata) {
348 reg = pdata->pad1;
349 ret = regmap_write(palmas->regmap[slave], addr, reg);
350 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200351 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900352 } else {
353 ret = regmap_read(palmas->regmap[slave], addr, &reg);
354 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200355 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900356 }
357
358 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0))
359 palmas->gpio_muxed |= PALMAS_GPIO_0_MUXED;
360 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK))
361 palmas->gpio_muxed |= PALMAS_GPIO_1_MUXED;
362 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
363 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
364 palmas->led_muxed |= PALMAS_LED1_MUXED;
365 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
366 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
367 palmas->pwm_muxed |= PALMAS_PWM1_MUXED;
368 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK))
369 palmas->gpio_muxed |= PALMAS_GPIO_2_MUXED;
370 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
371 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
372 palmas->led_muxed |= PALMAS_LED2_MUXED;
373 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
374 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
375 palmas->pwm_muxed |= PALMAS_PWM2_MUXED;
376 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3))
377 palmas->gpio_muxed |= PALMAS_GPIO_3_MUXED;
378
379 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
380 PALMAS_PRIMARY_SECONDARY_PAD2);
381
382 if (pdata->mux_from_pdata) {
383 reg = pdata->pad2;
384 ret = regmap_write(palmas->regmap[slave], addr, reg);
385 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200386 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900387 } else {
388 ret = regmap_read(palmas->regmap[slave], addr, &reg);
389 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200390 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900391 }
392
393 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4))
394 palmas->gpio_muxed |= PALMAS_GPIO_4_MUXED;
395 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK))
396 palmas->gpio_muxed |= PALMAS_GPIO_5_MUXED;
397 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6))
398 palmas->gpio_muxed |= PALMAS_GPIO_6_MUXED;
399 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK))
400 palmas->gpio_muxed |= PALMAS_GPIO_7_MUXED;
401
402 dev_info(palmas->dev, "Muxing GPIO %x, PWM %x, LED %x\n",
403 palmas->gpio_muxed, palmas->pwm_muxed,
404 palmas->led_muxed);
405
406 reg = pdata->power_ctrl;
407
408 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
409 addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_POWER_CTRL);
410
411 ret = regmap_write(palmas->regmap[slave], addr, reg);
412 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200413 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900414
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200415 /*
416 * If we are probing with DT do this the DT way and return here
417 * otherwise continue and add devices using mfd helpers.
418 */
419 if (node) {
420 ret = of_platform_populate(node, NULL, NULL, &i2c->dev);
421 if (ret < 0)
422 goto err_irq;
423 else
424 return ret;
425 }
426
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900427 return ret;
428
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200429err_irq:
430 regmap_del_irq_chip(palmas->irq, palmas->irq_data);
431err:
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900432 return ret;
433}
434
435static int palmas_i2c_remove(struct i2c_client *i2c)
436{
437 struct palmas *palmas = i2c_get_clientdata(i2c);
438
439 mfd_remove_devices(palmas->dev);
440 regmap_del_irq_chip(palmas->irq, palmas->irq_data);
441
442 return 0;
443}
444
445static const struct i2c_device_id palmas_i2c_id[] = {
446 { "palmas", },
447 { "twl6035", },
448 { "twl6037", },
449 { "tps65913", },
Axel Lin00ba81c2012-05-21 23:33:22 +0800450 { /* end */ }
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900451};
452MODULE_DEVICE_TABLE(i2c, palmas_i2c_id);
453
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900454static struct i2c_driver palmas_i2c_driver = {
455 .driver = {
456 .name = "palmas",
457 .of_match_table = of_palmas_match_tbl,
458 .owner = THIS_MODULE,
459 },
460 .probe = palmas_i2c_probe,
461 .remove = palmas_i2c_remove,
462 .id_table = palmas_i2c_id,
463};
464
465static int __init palmas_i2c_init(void)
466{
467 return i2c_add_driver(&palmas_i2c_driver);
468}
469/* init early so consumer devices can complete system boot */
470subsys_initcall(palmas_i2c_init);
471
472static void __exit palmas_i2c_exit(void)
473{
474 i2c_del_driver(&palmas_i2c_driver);
475}
476module_exit(palmas_i2c_exit);
477
478MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
479MODULE_DESCRIPTION("Palmas chip family multi-function driver");
480MODULE_LICENSE("GPL");