blob: 663a2398b6b1c878929940c6256c226b75adeec2 [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
Keerthy1c113d82014-06-18 15:28:55 +053049static const struct regmap_irq tps65917_irqs[] = {
50 /* INT1 IRQs */
51 [TPS65917_RESERVED1] = {
52 .mask = TPS65917_RESERVED,
53 },
54 [TPS65917_PWRON_IRQ] = {
55 .mask = TPS65917_INT1_STATUS_PWRON,
56 },
57 [TPS65917_LONG_PRESS_KEY_IRQ] = {
58 .mask = TPS65917_INT1_STATUS_LONG_PRESS_KEY,
59 },
60 [TPS65917_RESERVED2] = {
61 .mask = TPS65917_RESERVED,
62 },
63 [TPS65917_PWRDOWN_IRQ] = {
64 .mask = TPS65917_INT1_STATUS_PWRDOWN,
65 },
66 [TPS65917_HOTDIE_IRQ] = {
67 .mask = TPS65917_INT1_STATUS_HOTDIE,
68 },
69 [TPS65917_VSYS_MON_IRQ] = {
70 .mask = TPS65917_INT1_STATUS_VSYS_MON,
71 },
72 [TPS65917_RESERVED3] = {
73 .mask = TPS65917_RESERVED,
74 },
75 /* INT2 IRQs*/
76 [TPS65917_RESERVED4] = {
77 .mask = TPS65917_RESERVED,
78 .reg_offset = 1,
79 },
80 [TPS65917_OTP_ERROR_IRQ] = {
81 .mask = TPS65917_INT2_STATUS_OTP_ERROR,
82 .reg_offset = 1,
83 },
84 [TPS65917_WDT_IRQ] = {
85 .mask = TPS65917_INT2_STATUS_WDT,
86 .reg_offset = 1,
87 },
88 [TPS65917_RESERVED5] = {
89 .mask = TPS65917_RESERVED,
90 .reg_offset = 1,
91 },
92 [TPS65917_RESET_IN_IRQ] = {
93 .mask = TPS65917_INT2_STATUS_RESET_IN,
94 .reg_offset = 1,
95 },
96 [TPS65917_FSD_IRQ] = {
97 .mask = TPS65917_INT2_STATUS_FSD,
98 .reg_offset = 1,
99 },
100 [TPS65917_SHORT_IRQ] = {
101 .mask = TPS65917_INT2_STATUS_SHORT,
102 .reg_offset = 1,
103 },
104 [TPS65917_RESERVED6] = {
105 .mask = TPS65917_RESERVED,
106 .reg_offset = 1,
107 },
108 /* INT3 IRQs */
109 [TPS65917_GPADC_AUTO_0_IRQ] = {
110 .mask = TPS65917_INT3_STATUS_GPADC_AUTO_0,
111 .reg_offset = 2,
112 },
113 [TPS65917_GPADC_AUTO_1_IRQ] = {
114 .mask = TPS65917_INT3_STATUS_GPADC_AUTO_1,
115 .reg_offset = 2,
116 },
117 [TPS65917_GPADC_EOC_SW_IRQ] = {
118 .mask = TPS65917_INT3_STATUS_GPADC_EOC_SW,
119 .reg_offset = 2,
120 },
121 [TPS65917_RESREVED6] = {
122 .mask = TPS65917_RESERVED6,
123 .reg_offset = 2,
124 },
125 [TPS65917_RESERVED7] = {
126 .mask = TPS65917_RESERVED,
127 .reg_offset = 2,
128 },
129 [TPS65917_RESERVED8] = {
130 .mask = TPS65917_RESERVED,
131 .reg_offset = 2,
132 },
133 [TPS65917_RESERVED9] = {
134 .mask = TPS65917_RESERVED,
135 .reg_offset = 2,
136 },
137 [TPS65917_VBUS_IRQ] = {
138 .mask = TPS65917_INT3_STATUS_VBUS,
139 .reg_offset = 2,
140 },
141 /* INT4 IRQs */
142 [TPS65917_GPIO_0_IRQ] = {
143 .mask = TPS65917_INT4_STATUS_GPIO_0,
144 .reg_offset = 3,
145 },
146 [TPS65917_GPIO_1_IRQ] = {
147 .mask = TPS65917_INT4_STATUS_GPIO_1,
148 .reg_offset = 3,
149 },
150 [TPS65917_GPIO_2_IRQ] = {
151 .mask = TPS65917_INT4_STATUS_GPIO_2,
152 .reg_offset = 3,
153 },
154 [TPS65917_GPIO_3_IRQ] = {
155 .mask = TPS65917_INT4_STATUS_GPIO_3,
156 .reg_offset = 3,
157 },
158 [TPS65917_GPIO_4_IRQ] = {
159 .mask = TPS65917_INT4_STATUS_GPIO_4,
160 .reg_offset = 3,
161 },
162 [TPS65917_GPIO_5_IRQ] = {
163 .mask = TPS65917_INT4_STATUS_GPIO_5,
164 .reg_offset = 3,
165 },
166 [TPS65917_GPIO_6_IRQ] = {
167 .mask = TPS65917_INT4_STATUS_GPIO_6,
168 .reg_offset = 3,
169 },
170 [TPS65917_RESERVED10] = {
171 .mask = TPS65917_RESERVED10,
172 .reg_offset = 3,
173 },
174};
175
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900176static const struct regmap_irq palmas_irqs[] = {
177 /* INT1 IRQs */
178 [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ] = {
179 .mask = PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV,
180 },
181 [PALMAS_PWRON_IRQ] = {
182 .mask = PALMAS_INT1_STATUS_PWRON,
183 },
184 [PALMAS_LONG_PRESS_KEY_IRQ] = {
185 .mask = PALMAS_INT1_STATUS_LONG_PRESS_KEY,
186 },
187 [PALMAS_RPWRON_IRQ] = {
188 .mask = PALMAS_INT1_STATUS_RPWRON,
189 },
190 [PALMAS_PWRDOWN_IRQ] = {
191 .mask = PALMAS_INT1_STATUS_PWRDOWN,
192 },
193 [PALMAS_HOTDIE_IRQ] = {
194 .mask = PALMAS_INT1_STATUS_HOTDIE,
195 },
196 [PALMAS_VSYS_MON_IRQ] = {
197 .mask = PALMAS_INT1_STATUS_VSYS_MON,
198 },
199 [PALMAS_VBAT_MON_IRQ] = {
200 .mask = PALMAS_INT1_STATUS_VBAT_MON,
201 },
202 /* INT2 IRQs*/
203 [PALMAS_RTC_ALARM_IRQ] = {
204 .mask = PALMAS_INT2_STATUS_RTC_ALARM,
205 .reg_offset = 1,
206 },
207 [PALMAS_RTC_TIMER_IRQ] = {
208 .mask = PALMAS_INT2_STATUS_RTC_TIMER,
209 .reg_offset = 1,
210 },
211 [PALMAS_WDT_IRQ] = {
212 .mask = PALMAS_INT2_STATUS_WDT,
213 .reg_offset = 1,
214 },
215 [PALMAS_BATREMOVAL_IRQ] = {
216 .mask = PALMAS_INT2_STATUS_BATREMOVAL,
217 .reg_offset = 1,
218 },
219 [PALMAS_RESET_IN_IRQ] = {
220 .mask = PALMAS_INT2_STATUS_RESET_IN,
221 .reg_offset = 1,
222 },
223 [PALMAS_FBI_BB_IRQ] = {
224 .mask = PALMAS_INT2_STATUS_FBI_BB,
225 .reg_offset = 1,
226 },
227 [PALMAS_SHORT_IRQ] = {
228 .mask = PALMAS_INT2_STATUS_SHORT,
229 .reg_offset = 1,
230 },
231 [PALMAS_VAC_ACOK_IRQ] = {
232 .mask = PALMAS_INT2_STATUS_VAC_ACOK,
233 .reg_offset = 1,
234 },
235 /* INT3 IRQs */
236 [PALMAS_GPADC_AUTO_0_IRQ] = {
237 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_0,
238 .reg_offset = 2,
239 },
240 [PALMAS_GPADC_AUTO_1_IRQ] = {
241 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_1,
242 .reg_offset = 2,
243 },
244 [PALMAS_GPADC_EOC_SW_IRQ] = {
245 .mask = PALMAS_INT3_STATUS_GPADC_EOC_SW,
246 .reg_offset = 2,
247 },
248 [PALMAS_GPADC_EOC_RT_IRQ] = {
249 .mask = PALMAS_INT3_STATUS_GPADC_EOC_RT,
250 .reg_offset = 2,
251 },
252 [PALMAS_ID_OTG_IRQ] = {
253 .mask = PALMAS_INT3_STATUS_ID_OTG,
254 .reg_offset = 2,
255 },
256 [PALMAS_ID_IRQ] = {
257 .mask = PALMAS_INT3_STATUS_ID,
258 .reg_offset = 2,
259 },
260 [PALMAS_VBUS_OTG_IRQ] = {
261 .mask = PALMAS_INT3_STATUS_VBUS_OTG,
262 .reg_offset = 2,
263 },
264 [PALMAS_VBUS_IRQ] = {
265 .mask = PALMAS_INT3_STATUS_VBUS,
266 .reg_offset = 2,
267 },
268 /* INT4 IRQs */
269 [PALMAS_GPIO_0_IRQ] = {
270 .mask = PALMAS_INT4_STATUS_GPIO_0,
271 .reg_offset = 3,
272 },
273 [PALMAS_GPIO_1_IRQ] = {
274 .mask = PALMAS_INT4_STATUS_GPIO_1,
275 .reg_offset = 3,
276 },
277 [PALMAS_GPIO_2_IRQ] = {
278 .mask = PALMAS_INT4_STATUS_GPIO_2,
279 .reg_offset = 3,
280 },
281 [PALMAS_GPIO_3_IRQ] = {
282 .mask = PALMAS_INT4_STATUS_GPIO_3,
283 .reg_offset = 3,
284 },
285 [PALMAS_GPIO_4_IRQ] = {
286 .mask = PALMAS_INT4_STATUS_GPIO_4,
287 .reg_offset = 3,
288 },
289 [PALMAS_GPIO_5_IRQ] = {
290 .mask = PALMAS_INT4_STATUS_GPIO_5,
291 .reg_offset = 3,
292 },
293 [PALMAS_GPIO_6_IRQ] = {
294 .mask = PALMAS_INT4_STATUS_GPIO_6,
295 .reg_offset = 3,
296 },
297 [PALMAS_GPIO_7_IRQ] = {
298 .mask = PALMAS_INT4_STATUS_GPIO_7,
299 .reg_offset = 3,
300 },
301};
302
303static struct regmap_irq_chip palmas_irq_chip = {
304 .name = "palmas",
305 .irqs = palmas_irqs,
306 .num_irqs = ARRAY_SIZE(palmas_irqs),
307
308 .num_regs = 4,
309 .irq_reg_stride = 5,
310 .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
311 PALMAS_INT1_STATUS),
312 .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
313 PALMAS_INT1_MASK),
314};
315
Keerthy1c113d82014-06-18 15:28:55 +0530316static struct regmap_irq_chip tps65917_irq_chip = {
317 .name = "tps65917",
318 .irqs = tps65917_irqs,
319 .num_irqs = ARRAY_SIZE(tps65917_irqs),
320
321 .num_regs = 4,
322 .irq_reg_stride = 5,
323 .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
324 PALMAS_INT1_STATUS),
325 .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
326 PALMAS_INT1_MASK),
327};
328
Laxman Dewangancc01b462013-08-13 13:23:11 +0530329int palmas_ext_control_req_config(struct palmas *palmas,
330 enum palmas_external_requestor_id id, int ext_ctrl, bool enable)
331{
Keerthycac9e912014-06-18 15:28:59 +0530332 struct palmas_pmic_driver_data *pmic_ddata = palmas->pmic_ddata;
Laxman Dewangancc01b462013-08-13 13:23:11 +0530333 int preq_mask_bit = 0;
334 int reg_add = 0;
Keerthycac9e912014-06-18 15:28:59 +0530335 int bit_pos, ret;
Laxman Dewangancc01b462013-08-13 13:23:11 +0530336
337 if (!(ext_ctrl & PALMAS_EXT_REQ))
338 return 0;
339
340 if (id >= PALMAS_EXTERNAL_REQSTR_ID_MAX)
341 return 0;
342
343 if (ext_ctrl & PALMAS_EXT_CONTROL_NSLEEP) {
344 reg_add = PALMAS_NSLEEP_RES_ASSIGN;
345 preq_mask_bit = 0;
346 } else if (ext_ctrl & PALMAS_EXT_CONTROL_ENABLE1) {
347 reg_add = PALMAS_ENABLE1_RES_ASSIGN;
348 preq_mask_bit = 1;
349 } else if (ext_ctrl & PALMAS_EXT_CONTROL_ENABLE2) {
350 reg_add = PALMAS_ENABLE2_RES_ASSIGN;
351 preq_mask_bit = 2;
352 }
353
Keerthycac9e912014-06-18 15:28:59 +0530354 bit_pos = pmic_ddata->sleep_req_info[id].bit_pos;
355 reg_add += pmic_ddata->sleep_req_info[id].reg_offset;
Laxman Dewangancc01b462013-08-13 13:23:11 +0530356 if (enable)
357 ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
358 reg_add, BIT(bit_pos), BIT(bit_pos));
359 else
360 ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
361 reg_add, BIT(bit_pos), 0);
362 if (ret < 0) {
363 dev_err(palmas->dev, "Resource reg 0x%02x update failed %d\n",
364 reg_add, ret);
365 return ret;
366 }
367
368 /* Unmask the PREQ */
369 ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
370 PALMAS_POWER_CTRL, BIT(preq_mask_bit), 0);
371 if (ret < 0) {
372 dev_err(palmas->dev, "POWER_CTRL register update failed %d\n",
373 ret);
374 return ret;
375 }
376 return ret;
377}
378EXPORT_SYMBOL_GPL(palmas_ext_control_req_config);
379
Laxman Dewangandf545d12013-03-01 20:13:46 +0530380static int palmas_set_pdata_irq_flag(struct i2c_client *i2c,
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200381 struct palmas_platform_data *pdata)
382{
Laxman Dewangandf545d12013-03-01 20:13:46 +0530383 struct irq_data *irq_data = irq_get_irq_data(i2c->irq);
384 if (!irq_data) {
385 dev_err(&i2c->dev, "Invalid IRQ: %d\n", i2c->irq);
386 return -EINVAL;
387 }
388
389 pdata->irq_flags = irqd_get_trigger_type(irq_data);
390 dev_info(&i2c->dev, "Irq flag is 0x%08x\n", pdata->irq_flags);
391 return 0;
392}
393
394static void palmas_dt_to_pdata(struct i2c_client *i2c,
395 struct palmas_platform_data *pdata)
396{
397 struct device_node *node = i2c->dev.of_node;
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200398 int ret;
399 u32 prop;
400
J Keerthy2154a2b2013-02-18 10:42:44 +0530401 ret = of_property_read_u32(node, "ti,mux-pad1", &prop);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200402 if (!ret) {
403 pdata->mux_from_pdata = 1;
404 pdata->pad1 = prop;
405 }
406
J Keerthy2154a2b2013-02-18 10:42:44 +0530407 ret = of_property_read_u32(node, "ti,mux-pad2", &prop);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200408 if (!ret) {
409 pdata->mux_from_pdata = 1;
410 pdata->pad2 = prop;
411 }
412
413 /* The default for this register is all masked */
J Keerthy2154a2b2013-02-18 10:42:44 +0530414 ret = of_property_read_u32(node, "ti,power-ctrl", &prop);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200415 if (!ret)
416 pdata->power_ctrl = prop;
417 else
418 pdata->power_ctrl = PALMAS_POWER_CTRL_NSLEEP_MASK |
419 PALMAS_POWER_CTRL_ENABLE1_MASK |
420 PALMAS_POWER_CTRL_ENABLE2_MASK;
Laxman Dewangandf545d12013-03-01 20:13:46 +0530421 if (i2c->irq)
422 palmas_set_pdata_irq_flag(i2c, pdata);
Bill Huangb81eec02013-08-08 04:45:05 -0700423
424 pdata->pm_off = of_property_read_bool(node,
425 "ti,system-power-controller");
426}
427
428static struct palmas *palmas_dev;
429static void palmas_power_off(void)
430{
431 unsigned int addr;
432 int ret, slave;
Keerthy572ff4d2017-10-24 13:51:36 +0530433 u8 powerhold_mask;
Keerthy85fdaf82016-11-10 10:39:18 +0530434 struct device_node *np = palmas_dev->dev->of_node;
435
436 if (of_property_read_bool(np, "ti,palmas-override-powerhold")) {
437 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
438 PALMAS_PRIMARY_SECONDARY_PAD2);
439 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
440
Keerthy572ff4d2017-10-24 13:51:36 +0530441 if (of_device_is_compatible(np, "ti,tps65917"))
442 powerhold_mask =
443 TPS65917_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK;
444 else
445 powerhold_mask =
446 PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK;
447
Keerthy85fdaf82016-11-10 10:39:18 +0530448 ret = regmap_update_bits(palmas_dev->regmap[slave], addr,
Keerthy572ff4d2017-10-24 13:51:36 +0530449 powerhold_mask, 0);
Keerthy85fdaf82016-11-10 10:39:18 +0530450 if (ret)
451 dev_err(palmas_dev->dev,
452 "Unable to write PRIMARY_SECONDARY_PAD2 %d\n",
453 ret);
454 }
Bill Huangb81eec02013-08-08 04:45:05 -0700455
Bill Huangb81eec02013-08-08 04:45:05 -0700456 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
457 addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_DEV_CTRL);
458
459 ret = regmap_update_bits(
460 palmas_dev->regmap[slave],
461 addr,
462 PALMAS_DEV_CTRL_DEV_ON,
463 0);
464
465 if (ret)
466 pr_err("%s: Unable to write to DEV_CTRL_DEV_ON: %d\n",
467 __func__, ret);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200468}
469
J Keerthy1ffb0be2013-06-19 11:27:48 +0530470static unsigned int palmas_features = PALMAS_PMIC_FEATURE_SMPS10_BOOST;
J Keerthy4124e6e2013-06-20 16:35:16 +0530471static unsigned int tps659038_features;
J Keerthy1ffb0be2013-06-19 11:27:48 +0530472
Keerthy1c113d82014-06-18 15:28:55 +0530473struct palmas_driver_data {
474 unsigned int *features;
475 struct regmap_irq_chip *irq_chip;
476};
477
478static struct palmas_driver_data palmas_data = {
479 .features = &palmas_features,
480 .irq_chip = &palmas_irq_chip,
481};
482
483static struct palmas_driver_data tps659038_data = {
484 .features = &tps659038_features,
485 .irq_chip = &palmas_irq_chip,
486};
487
488static struct palmas_driver_data tps65917_data = {
489 .features = &tps659038_features,
490 .irq_chip = &tps65917_irq_chip,
491};
492
J Keerthy1ffb0be2013-06-19 11:27:48 +0530493static const struct of_device_id of_palmas_match_tbl[] = {
494 {
495 .compatible = "ti,palmas",
Keerthy1c113d82014-06-18 15:28:55 +0530496 .data = &palmas_data,
J Keerthy1ffb0be2013-06-19 11:27:48 +0530497 },
J Keerthy4124e6e2013-06-20 16:35:16 +0530498 {
499 .compatible = "ti,tps659038",
Keerthy1c113d82014-06-18 15:28:55 +0530500 .data = &tps659038_data,
501 },
502 {
503 .compatible = "ti,tps65917",
504 .data = &tps65917_data,
J Keerthy4124e6e2013-06-20 16:35:16 +0530505 },
J Keerthy1ffb0be2013-06-19 11:27:48 +0530506 { },
507};
Laxman Dewangan2d8edaf2013-09-26 19:03:49 +0530508MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);
J Keerthy1ffb0be2013-06-19 11:27:48 +0530509
Bill Pembertonf791be42012-11-19 13:23:04 -0500510static int palmas_i2c_probe(struct i2c_client *i2c,
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900511 const struct i2c_device_id *id)
512{
513 struct palmas *palmas;
514 struct palmas_platform_data *pdata;
Keerthy1c113d82014-06-18 15:28:55 +0530515 struct palmas_driver_data *driver_data;
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200516 struct device_node *node = i2c->dev.of_node;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900517 int ret = 0, i;
Keerthy1c113d82014-06-18 15:28:55 +0530518 unsigned int reg, addr;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900519 int slave;
J Keerthy1ffb0be2013-06-19 11:27:48 +0530520 const struct of_device_id *match;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900521
522 pdata = dev_get_platdata(&i2c->dev);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200523
524 if (node && !pdata) {
525 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
526
527 if (!pdata)
528 return -ENOMEM;
529
Laxman Dewangandf545d12013-03-01 20:13:46 +0530530 palmas_dt_to_pdata(i2c, pdata);
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200531 }
532
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900533 if (!pdata)
534 return -EINVAL;
535
536 palmas = devm_kzalloc(&i2c->dev, sizeof(struct palmas), GFP_KERNEL);
537 if (palmas == NULL)
538 return -ENOMEM;
539
540 i2c_set_clientdata(i2c, palmas);
541 palmas->dev = &i2c->dev;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900542 palmas->irq = i2c->irq;
543
Sachin Kamat84195b72013-10-15 09:18:51 +0530544 match = of_match_device(of_palmas_match_tbl, &i2c->dev);
J Keerthy1ffb0be2013-06-19 11:27:48 +0530545
546 if (!match)
547 return -ENODATA;
548
Keerthy1c113d82014-06-18 15:28:55 +0530549 driver_data = (struct palmas_driver_data *)match->data;
550 palmas->features = *driver_data->features;
J Keerthy1ffb0be2013-06-19 11:27:48 +0530551
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900552 for (i = 0; i < PALMAS_NUM_CLIENTS; i++) {
553 if (i == 0)
554 palmas->i2c_clients[i] = i2c;
555 else {
556 palmas->i2c_clients[i] =
557 i2c_new_dummy(i2c->adapter,
558 i2c->addr + i);
559 if (!palmas->i2c_clients[i]) {
560 dev_err(palmas->dev,
561 "can't attach client %d\n", i);
562 ret = -ENOMEM;
Laxman Dewangan5e172d72013-09-26 19:03:51 +0530563 goto err_i2c;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900564 }
Laxman Dewanganc4fbec32013-03-19 14:28:20 +0530565 palmas->i2c_clients[i]->dev.of_node = of_node_get(node);
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900566 }
567 palmas->regmap[i] = devm_regmap_init_i2c(palmas->i2c_clients[i],
568 &palmas_regmap_config[i]);
569 if (IS_ERR(palmas->regmap[i])) {
570 ret = PTR_ERR(palmas->regmap[i]);
571 dev_err(palmas->dev,
572 "Failed to allocate regmap %d, err: %d\n",
573 i, ret);
Laxman Dewangan5e172d72013-09-26 19:03:51 +0530574 goto err_i2c;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900575 }
576 }
577
J Keerthyad522f42013-06-19 11:27:47 +0530578 if (!palmas->irq) {
579 dev_warn(palmas->dev, "IRQ missing: skipping irq request\n");
580 goto no_irq;
581 }
582
Laxman Dewangandf545d12013-03-01 20:13:46 +0530583 /* Change interrupt line output polarity */
584 if (pdata->irq_flags & IRQ_TYPE_LEVEL_HIGH)
585 reg = PALMAS_POLARITY_CTRL_INT_POLARITY;
586 else
587 reg = 0;
588 ret = palmas_update_bits(palmas, PALMAS_PU_PD_OD_BASE,
589 PALMAS_POLARITY_CTRL, PALMAS_POLARITY_CTRL_INT_POLARITY,
590 reg);
591 if (ret < 0) {
Willis Monroeb5b086a2017-04-11 09:10:06 -0700592 dev_err(palmas->dev, "POLARITY_CTRL update failed: %d\n", ret);
Laxman Dewangan5e172d72013-09-26 19:03:51 +0530593 goto err_i2c;
Laxman Dewangandf545d12013-03-01 20:13:46 +0530594 }
595
Graeme Gregoryb330f852012-06-22 13:36:19 +0100596 /* Change IRQ into clear on read mode for efficiency */
597 slave = PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE);
598 addr = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, PALMAS_INT_CTRL);
599 reg = PALMAS_INT_CTRL_INT_CLEAR;
600
601 regmap_write(palmas->regmap[slave], addr, reg);
602
603 ret = regmap_add_irq_chip(palmas->regmap[slave], palmas->irq,
Keerthy1c113d82014-06-18 15:28:55 +0530604 IRQF_ONESHOT | pdata->irq_flags, 0,
605 driver_data->irq_chip, &palmas->irq_data);
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900606 if (ret < 0)
Laxman Dewangan5e172d72013-09-26 19:03:51 +0530607 goto err_i2c;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900608
J Keerthyad522f42013-06-19 11:27:47 +0530609no_irq:
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900610 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
611 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
612 PALMAS_PRIMARY_SECONDARY_PAD1);
613
614 if (pdata->mux_from_pdata) {
615 reg = pdata->pad1;
616 ret = regmap_write(palmas->regmap[slave], addr, reg);
617 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200618 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900619 } else {
620 ret = regmap_read(palmas->regmap[slave], addr, &reg);
621 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200622 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900623 }
624
625 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0))
626 palmas->gpio_muxed |= PALMAS_GPIO_0_MUXED;
627 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK))
628 palmas->gpio_muxed |= PALMAS_GPIO_1_MUXED;
629 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
630 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
631 palmas->led_muxed |= PALMAS_LED1_MUXED;
632 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
633 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
634 palmas->pwm_muxed |= PALMAS_PWM1_MUXED;
635 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK))
636 palmas->gpio_muxed |= PALMAS_GPIO_2_MUXED;
637 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
638 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
639 palmas->led_muxed |= PALMAS_LED2_MUXED;
640 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
641 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
642 palmas->pwm_muxed |= PALMAS_PWM2_MUXED;
643 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3))
644 palmas->gpio_muxed |= PALMAS_GPIO_3_MUXED;
645
646 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
647 PALMAS_PRIMARY_SECONDARY_PAD2);
648
649 if (pdata->mux_from_pdata) {
650 reg = pdata->pad2;
651 ret = regmap_write(palmas->regmap[slave], addr, reg);
652 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200653 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900654 } else {
655 ret = regmap_read(palmas->regmap[slave], addr, &reg);
656 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200657 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900658 }
659
660 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4))
661 palmas->gpio_muxed |= PALMAS_GPIO_4_MUXED;
662 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK))
663 palmas->gpio_muxed |= PALMAS_GPIO_5_MUXED;
664 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6))
665 palmas->gpio_muxed |= PALMAS_GPIO_6_MUXED;
666 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK))
667 palmas->gpio_muxed |= PALMAS_GPIO_7_MUXED;
668
669 dev_info(palmas->dev, "Muxing GPIO %x, PWM %x, LED %x\n",
670 palmas->gpio_muxed, palmas->pwm_muxed,
671 palmas->led_muxed);
672
673 reg = pdata->power_ctrl;
674
675 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
676 addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_POWER_CTRL);
677
678 ret = regmap_write(palmas->regmap[slave], addr, reg);
679 if (ret)
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200680 goto err_irq;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900681
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200682 /*
683 * If we are probing with DT do this the DT way and return here
684 * otherwise continue and add devices using mfd helpers.
685 */
686 if (node) {
Benjamin Gaignard124e9de2017-05-29 17:45:59 +0200687 ret = devm_of_platform_populate(&i2c->dev);
Bill Huangb81eec02013-08-08 04:45:05 -0700688 if (ret < 0) {
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200689 goto err_irq;
Bill Huangb81eec02013-08-08 04:45:05 -0700690 } else if (pdata->pm_off && !pm_power_off) {
691 palmas_dev = palmas;
692 pm_power_off = palmas_power_off;
Bill Huangb81eec02013-08-08 04:45:05 -0700693 }
Graeme Gregory9c14ac32012-08-28 13:47:38 +0200694 }
695
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900696 return ret;
697
Graeme Gregory3f78dec2012-08-28 13:47:35 +0200698err_irq:
699 regmap_del_irq_chip(palmas->irq, palmas->irq_data);
Laxman Dewangan5e172d72013-09-26 19:03:51 +0530700err_i2c:
701 for (i = 1; i < PALMAS_NUM_CLIENTS; i++) {
702 if (palmas->i2c_clients[i])
703 i2c_unregister_device(palmas->i2c_clients[i]);
704 }
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900705 return ret;
706}
707
708static int palmas_i2c_remove(struct i2c_client *i2c)
709{
710 struct palmas *palmas = i2c_get_clientdata(i2c);
Laxman Dewangan5e172d72013-09-26 19:03:51 +0530711 int i;
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900712
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900713 regmap_del_irq_chip(palmas->irq, palmas->irq_data);
714
Laxman Dewangan5e172d72013-09-26 19:03:51 +0530715 for (i = 1; i < PALMAS_NUM_CLIENTS; i++) {
716 if (palmas->i2c_clients[i])
717 i2c_unregister_device(palmas->i2c_clients[i]);
718 }
719
Laxman Dewangan71783472013-09-26 19:03:50 +0530720 if (palmas == palmas_dev) {
721 pm_power_off = NULL;
722 palmas_dev = NULL;
723 }
724
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900725 return 0;
726}
727
728static const struct i2c_device_id palmas_i2c_id[] = {
729 { "palmas", },
730 { "twl6035", },
731 { "twl6037", },
732 { "tps65913", },
Axel Lin00ba81c2012-05-21 23:33:22 +0800733 { /* end */ }
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900734};
735MODULE_DEVICE_TABLE(i2c, palmas_i2c_id);
736
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900737static struct i2c_driver palmas_i2c_driver = {
738 .driver = {
739 .name = "palmas",
740 .of_match_table = of_palmas_match_tbl,
Graeme Gregory2945fbc2012-05-15 15:48:56 +0900741 },
742 .probe = palmas_i2c_probe,
743 .remove = palmas_i2c_remove,
744 .id_table = palmas_i2c_id,
745};
746
747static int __init palmas_i2c_init(void)
748{
749 return i2c_add_driver(&palmas_i2c_driver);
750}
751/* init early so consumer devices can complete system boot */
752subsys_initcall(palmas_i2c_init);
753
754static void __exit palmas_i2c_exit(void)
755{
756 i2c_del_driver(&palmas_i2c_driver);
757}
758module_exit(palmas_i2c_exit);
759
760MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
761MODULE_DESCRIPTION("Palmas chip family multi-function driver");
762MODULE_LICENSE("GPL");