palmas.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  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. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/regmap.h>
  22. #include <linux/err.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/mfd/palmas.h>
  25. #include <linux/of_device.h>
  26. static const struct regmap_config palmas_regmap_config[PALMAS_NUM_CLIENTS] = {
  27. {
  28. .reg_bits = 8,
  29. .val_bits = 8,
  30. .max_register = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
  31. PALMAS_PRIMARY_SECONDARY_PAD3),
  32. },
  33. {
  34. .reg_bits = 8,
  35. .val_bits = 8,
  36. .max_register = PALMAS_BASE_TO_REG(PALMAS_GPADC_BASE,
  37. PALMAS_GPADC_SMPS_VSEL_MONITORING),
  38. },
  39. {
  40. .reg_bits = 8,
  41. .val_bits = 8,
  42. .max_register = PALMAS_BASE_TO_REG(PALMAS_TRIM_GPADC_BASE,
  43. PALMAS_GPADC_TRIM16),
  44. },
  45. };
  46. static const struct regmap_irq tps65917_irqs[] = {
  47. /* INT1 IRQs */
  48. [TPS65917_RESERVED1] = {
  49. .mask = TPS65917_RESERVED,
  50. },
  51. [TPS65917_PWRON_IRQ] = {
  52. .mask = TPS65917_INT1_STATUS_PWRON,
  53. },
  54. [TPS65917_LONG_PRESS_KEY_IRQ] = {
  55. .mask = TPS65917_INT1_STATUS_LONG_PRESS_KEY,
  56. },
  57. [TPS65917_RESERVED2] = {
  58. .mask = TPS65917_RESERVED,
  59. },
  60. [TPS65917_PWRDOWN_IRQ] = {
  61. .mask = TPS65917_INT1_STATUS_PWRDOWN,
  62. },
  63. [TPS65917_HOTDIE_IRQ] = {
  64. .mask = TPS65917_INT1_STATUS_HOTDIE,
  65. },
  66. [TPS65917_VSYS_MON_IRQ] = {
  67. .mask = TPS65917_INT1_STATUS_VSYS_MON,
  68. },
  69. [TPS65917_RESERVED3] = {
  70. .mask = TPS65917_RESERVED,
  71. },
  72. /* INT2 IRQs*/
  73. [TPS65917_RESERVED4] = {
  74. .mask = TPS65917_RESERVED,
  75. .reg_offset = 1,
  76. },
  77. [TPS65917_OTP_ERROR_IRQ] = {
  78. .mask = TPS65917_INT2_STATUS_OTP_ERROR,
  79. .reg_offset = 1,
  80. },
  81. [TPS65917_WDT_IRQ] = {
  82. .mask = TPS65917_INT2_STATUS_WDT,
  83. .reg_offset = 1,
  84. },
  85. [TPS65917_RESERVED5] = {
  86. .mask = TPS65917_RESERVED,
  87. .reg_offset = 1,
  88. },
  89. [TPS65917_RESET_IN_IRQ] = {
  90. .mask = TPS65917_INT2_STATUS_RESET_IN,
  91. .reg_offset = 1,
  92. },
  93. [TPS65917_FSD_IRQ] = {
  94. .mask = TPS65917_INT2_STATUS_FSD,
  95. .reg_offset = 1,
  96. },
  97. [TPS65917_SHORT_IRQ] = {
  98. .mask = TPS65917_INT2_STATUS_SHORT,
  99. .reg_offset = 1,
  100. },
  101. [TPS65917_RESERVED6] = {
  102. .mask = TPS65917_RESERVED,
  103. .reg_offset = 1,
  104. },
  105. /* INT3 IRQs */
  106. [TPS65917_GPADC_AUTO_0_IRQ] = {
  107. .mask = TPS65917_INT3_STATUS_GPADC_AUTO_0,
  108. .reg_offset = 2,
  109. },
  110. [TPS65917_GPADC_AUTO_1_IRQ] = {
  111. .mask = TPS65917_INT3_STATUS_GPADC_AUTO_1,
  112. .reg_offset = 2,
  113. },
  114. [TPS65917_GPADC_EOC_SW_IRQ] = {
  115. .mask = TPS65917_INT3_STATUS_GPADC_EOC_SW,
  116. .reg_offset = 2,
  117. },
  118. [TPS65917_RESREVED6] = {
  119. .mask = TPS65917_RESERVED6,
  120. .reg_offset = 2,
  121. },
  122. [TPS65917_RESERVED7] = {
  123. .mask = TPS65917_RESERVED,
  124. .reg_offset = 2,
  125. },
  126. [TPS65917_RESERVED8] = {
  127. .mask = TPS65917_RESERVED,
  128. .reg_offset = 2,
  129. },
  130. [TPS65917_RESERVED9] = {
  131. .mask = TPS65917_RESERVED,
  132. .reg_offset = 2,
  133. },
  134. [TPS65917_VBUS_IRQ] = {
  135. .mask = TPS65917_INT3_STATUS_VBUS,
  136. .reg_offset = 2,
  137. },
  138. /* INT4 IRQs */
  139. [TPS65917_GPIO_0_IRQ] = {
  140. .mask = TPS65917_INT4_STATUS_GPIO_0,
  141. .reg_offset = 3,
  142. },
  143. [TPS65917_GPIO_1_IRQ] = {
  144. .mask = TPS65917_INT4_STATUS_GPIO_1,
  145. .reg_offset = 3,
  146. },
  147. [TPS65917_GPIO_2_IRQ] = {
  148. .mask = TPS65917_INT4_STATUS_GPIO_2,
  149. .reg_offset = 3,
  150. },
  151. [TPS65917_GPIO_3_IRQ] = {
  152. .mask = TPS65917_INT4_STATUS_GPIO_3,
  153. .reg_offset = 3,
  154. },
  155. [TPS65917_GPIO_4_IRQ] = {
  156. .mask = TPS65917_INT4_STATUS_GPIO_4,
  157. .reg_offset = 3,
  158. },
  159. [TPS65917_GPIO_5_IRQ] = {
  160. .mask = TPS65917_INT4_STATUS_GPIO_5,
  161. .reg_offset = 3,
  162. },
  163. [TPS65917_GPIO_6_IRQ] = {
  164. .mask = TPS65917_INT4_STATUS_GPIO_6,
  165. .reg_offset = 3,
  166. },
  167. [TPS65917_RESERVED10] = {
  168. .mask = TPS65917_RESERVED10,
  169. .reg_offset = 3,
  170. },
  171. };
  172. static const struct regmap_irq palmas_irqs[] = {
  173. /* INT1 IRQs */
  174. [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ] = {
  175. .mask = PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV,
  176. },
  177. [PALMAS_PWRON_IRQ] = {
  178. .mask = PALMAS_INT1_STATUS_PWRON,
  179. },
  180. [PALMAS_LONG_PRESS_KEY_IRQ] = {
  181. .mask = PALMAS_INT1_STATUS_LONG_PRESS_KEY,
  182. },
  183. [PALMAS_RPWRON_IRQ] = {
  184. .mask = PALMAS_INT1_STATUS_RPWRON,
  185. },
  186. [PALMAS_PWRDOWN_IRQ] = {
  187. .mask = PALMAS_INT1_STATUS_PWRDOWN,
  188. },
  189. [PALMAS_HOTDIE_IRQ] = {
  190. .mask = PALMAS_INT1_STATUS_HOTDIE,
  191. },
  192. [PALMAS_VSYS_MON_IRQ] = {
  193. .mask = PALMAS_INT1_STATUS_VSYS_MON,
  194. },
  195. [PALMAS_VBAT_MON_IRQ] = {
  196. .mask = PALMAS_INT1_STATUS_VBAT_MON,
  197. },
  198. /* INT2 IRQs*/
  199. [PALMAS_RTC_ALARM_IRQ] = {
  200. .mask = PALMAS_INT2_STATUS_RTC_ALARM,
  201. .reg_offset = 1,
  202. },
  203. [PALMAS_RTC_TIMER_IRQ] = {
  204. .mask = PALMAS_INT2_STATUS_RTC_TIMER,
  205. .reg_offset = 1,
  206. },
  207. [PALMAS_WDT_IRQ] = {
  208. .mask = PALMAS_INT2_STATUS_WDT,
  209. .reg_offset = 1,
  210. },
  211. [PALMAS_BATREMOVAL_IRQ] = {
  212. .mask = PALMAS_INT2_STATUS_BATREMOVAL,
  213. .reg_offset = 1,
  214. },
  215. [PALMAS_RESET_IN_IRQ] = {
  216. .mask = PALMAS_INT2_STATUS_RESET_IN,
  217. .reg_offset = 1,
  218. },
  219. [PALMAS_FBI_BB_IRQ] = {
  220. .mask = PALMAS_INT2_STATUS_FBI_BB,
  221. .reg_offset = 1,
  222. },
  223. [PALMAS_SHORT_IRQ] = {
  224. .mask = PALMAS_INT2_STATUS_SHORT,
  225. .reg_offset = 1,
  226. },
  227. [PALMAS_VAC_ACOK_IRQ] = {
  228. .mask = PALMAS_INT2_STATUS_VAC_ACOK,
  229. .reg_offset = 1,
  230. },
  231. /* INT3 IRQs */
  232. [PALMAS_GPADC_AUTO_0_IRQ] = {
  233. .mask = PALMAS_INT3_STATUS_GPADC_AUTO_0,
  234. .reg_offset = 2,
  235. },
  236. [PALMAS_GPADC_AUTO_1_IRQ] = {
  237. .mask = PALMAS_INT3_STATUS_GPADC_AUTO_1,
  238. .reg_offset = 2,
  239. },
  240. [PALMAS_GPADC_EOC_SW_IRQ] = {
  241. .mask = PALMAS_INT3_STATUS_GPADC_EOC_SW,
  242. .reg_offset = 2,
  243. },
  244. [PALMAS_GPADC_EOC_RT_IRQ] = {
  245. .mask = PALMAS_INT3_STATUS_GPADC_EOC_RT,
  246. .reg_offset = 2,
  247. },
  248. [PALMAS_ID_OTG_IRQ] = {
  249. .mask = PALMAS_INT3_STATUS_ID_OTG,
  250. .reg_offset = 2,
  251. },
  252. [PALMAS_ID_IRQ] = {
  253. .mask = PALMAS_INT3_STATUS_ID,
  254. .reg_offset = 2,
  255. },
  256. [PALMAS_VBUS_OTG_IRQ] = {
  257. .mask = PALMAS_INT3_STATUS_VBUS_OTG,
  258. .reg_offset = 2,
  259. },
  260. [PALMAS_VBUS_IRQ] = {
  261. .mask = PALMAS_INT3_STATUS_VBUS,
  262. .reg_offset = 2,
  263. },
  264. /* INT4 IRQs */
  265. [PALMAS_GPIO_0_IRQ] = {
  266. .mask = PALMAS_INT4_STATUS_GPIO_0,
  267. .reg_offset = 3,
  268. },
  269. [PALMAS_GPIO_1_IRQ] = {
  270. .mask = PALMAS_INT4_STATUS_GPIO_1,
  271. .reg_offset = 3,
  272. },
  273. [PALMAS_GPIO_2_IRQ] = {
  274. .mask = PALMAS_INT4_STATUS_GPIO_2,
  275. .reg_offset = 3,
  276. },
  277. [PALMAS_GPIO_3_IRQ] = {
  278. .mask = PALMAS_INT4_STATUS_GPIO_3,
  279. .reg_offset = 3,
  280. },
  281. [PALMAS_GPIO_4_IRQ] = {
  282. .mask = PALMAS_INT4_STATUS_GPIO_4,
  283. .reg_offset = 3,
  284. },
  285. [PALMAS_GPIO_5_IRQ] = {
  286. .mask = PALMAS_INT4_STATUS_GPIO_5,
  287. .reg_offset = 3,
  288. },
  289. [PALMAS_GPIO_6_IRQ] = {
  290. .mask = PALMAS_INT4_STATUS_GPIO_6,
  291. .reg_offset = 3,
  292. },
  293. [PALMAS_GPIO_7_IRQ] = {
  294. .mask = PALMAS_INT4_STATUS_GPIO_7,
  295. .reg_offset = 3,
  296. },
  297. };
  298. static struct regmap_irq_chip palmas_irq_chip = {
  299. .name = "palmas",
  300. .irqs = palmas_irqs,
  301. .num_irqs = ARRAY_SIZE(palmas_irqs),
  302. .num_regs = 4,
  303. .irq_reg_stride = 5,
  304. .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
  305. PALMAS_INT1_STATUS),
  306. .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
  307. PALMAS_INT1_MASK),
  308. };
  309. static struct regmap_irq_chip tps65917_irq_chip = {
  310. .name = "tps65917",
  311. .irqs = tps65917_irqs,
  312. .num_irqs = ARRAY_SIZE(tps65917_irqs),
  313. .num_regs = 4,
  314. .irq_reg_stride = 5,
  315. .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
  316. PALMAS_INT1_STATUS),
  317. .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
  318. PALMAS_INT1_MASK),
  319. };
  320. int palmas_ext_control_req_config(struct palmas *palmas,
  321. enum palmas_external_requestor_id id, int ext_ctrl, bool enable)
  322. {
  323. struct palmas_pmic_driver_data *pmic_ddata = palmas->pmic_ddata;
  324. int preq_mask_bit = 0;
  325. int reg_add = 0;
  326. int bit_pos, ret;
  327. if (!(ext_ctrl & PALMAS_EXT_REQ))
  328. return 0;
  329. if (id >= PALMAS_EXTERNAL_REQSTR_ID_MAX)
  330. return 0;
  331. if (ext_ctrl & PALMAS_EXT_CONTROL_NSLEEP) {
  332. reg_add = PALMAS_NSLEEP_RES_ASSIGN;
  333. preq_mask_bit = 0;
  334. } else if (ext_ctrl & PALMAS_EXT_CONTROL_ENABLE1) {
  335. reg_add = PALMAS_ENABLE1_RES_ASSIGN;
  336. preq_mask_bit = 1;
  337. } else if (ext_ctrl & PALMAS_EXT_CONTROL_ENABLE2) {
  338. reg_add = PALMAS_ENABLE2_RES_ASSIGN;
  339. preq_mask_bit = 2;
  340. }
  341. bit_pos = pmic_ddata->sleep_req_info[id].bit_pos;
  342. reg_add += pmic_ddata->sleep_req_info[id].reg_offset;
  343. if (enable)
  344. ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
  345. reg_add, BIT(bit_pos), BIT(bit_pos));
  346. else
  347. ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
  348. reg_add, BIT(bit_pos), 0);
  349. if (ret < 0) {
  350. dev_err(palmas->dev, "Resource reg 0x%02x update failed %d\n",
  351. reg_add, ret);
  352. return ret;
  353. }
  354. /* Unmask the PREQ */
  355. ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
  356. PALMAS_POWER_CTRL, BIT(preq_mask_bit), 0);
  357. if (ret < 0) {
  358. dev_err(palmas->dev, "POWER_CTRL register update failed %d\n",
  359. ret);
  360. return ret;
  361. }
  362. return ret;
  363. }
  364. EXPORT_SYMBOL_GPL(palmas_ext_control_req_config);
  365. static int palmas_set_pdata_irq_flag(struct i2c_client *i2c,
  366. struct palmas_platform_data *pdata)
  367. {
  368. struct irq_data *irq_data = irq_get_irq_data(i2c->irq);
  369. if (!irq_data) {
  370. dev_err(&i2c->dev, "Invalid IRQ: %d\n", i2c->irq);
  371. return -EINVAL;
  372. }
  373. pdata->irq_flags = irqd_get_trigger_type(irq_data);
  374. dev_info(&i2c->dev, "Irq flag is 0x%08x\n", pdata->irq_flags);
  375. return 0;
  376. }
  377. static void palmas_dt_to_pdata(struct i2c_client *i2c,
  378. struct palmas_platform_data *pdata)
  379. {
  380. struct device_node *node = i2c->dev.of_node;
  381. int ret;
  382. u32 prop;
  383. ret = of_property_read_u32(node, "ti,mux-pad1", &prop);
  384. if (!ret) {
  385. pdata->mux_from_pdata = 1;
  386. pdata->pad1 = prop;
  387. }
  388. ret = of_property_read_u32(node, "ti,mux-pad2", &prop);
  389. if (!ret) {
  390. pdata->mux_from_pdata = 1;
  391. pdata->pad2 = prop;
  392. }
  393. /* The default for this register is all masked */
  394. ret = of_property_read_u32(node, "ti,power-ctrl", &prop);
  395. if (!ret)
  396. pdata->power_ctrl = prop;
  397. else
  398. pdata->power_ctrl = PALMAS_POWER_CTRL_NSLEEP_MASK |
  399. PALMAS_POWER_CTRL_ENABLE1_MASK |
  400. PALMAS_POWER_CTRL_ENABLE2_MASK;
  401. if (i2c->irq)
  402. palmas_set_pdata_irq_flag(i2c, pdata);
  403. pdata->pm_off = of_property_read_bool(node,
  404. "ti,system-power-controller");
  405. }
  406. static struct palmas *palmas_dev;
  407. static void palmas_power_off(void)
  408. {
  409. unsigned int addr;
  410. int ret, slave;
  411. struct device_node *np = palmas_dev->dev->of_node;
  412. if (of_property_read_bool(np, "ti,palmas-override-powerhold")) {
  413. addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
  414. PALMAS_PRIMARY_SECONDARY_PAD2);
  415. slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
  416. ret = regmap_update_bits(palmas_dev->regmap[slave], addr,
  417. PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK, 0);
  418. if (ret)
  419. dev_err(palmas_dev->dev,
  420. "Unable to write PRIMARY_SECONDARY_PAD2 %d\n",
  421. ret);
  422. }
  423. if (!palmas_dev)
  424. return;
  425. slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
  426. addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_DEV_CTRL);
  427. ret = regmap_update_bits(
  428. palmas_dev->regmap[slave],
  429. addr,
  430. PALMAS_DEV_CTRL_DEV_ON,
  431. 0);
  432. if (ret)
  433. pr_err("%s: Unable to write to DEV_CTRL_DEV_ON: %d\n",
  434. __func__, ret);
  435. }
  436. static unsigned int palmas_features = PALMAS_PMIC_FEATURE_SMPS10_BOOST;
  437. static unsigned int tps659038_features;
  438. struct palmas_driver_data {
  439. unsigned int *features;
  440. struct regmap_irq_chip *irq_chip;
  441. };
  442. static struct palmas_driver_data palmas_data = {
  443. .features = &palmas_features,
  444. .irq_chip = &palmas_irq_chip,
  445. };
  446. static struct palmas_driver_data tps659038_data = {
  447. .features = &tps659038_features,
  448. .irq_chip = &palmas_irq_chip,
  449. };
  450. static struct palmas_driver_data tps65917_data = {
  451. .features = &tps659038_features,
  452. .irq_chip = &tps65917_irq_chip,
  453. };
  454. static const struct of_device_id of_palmas_match_tbl[] = {
  455. {
  456. .compatible = "ti,palmas",
  457. .data = &palmas_data,
  458. },
  459. {
  460. .compatible = "ti,tps659038",
  461. .data = &tps659038_data,
  462. },
  463. {
  464. .compatible = "ti,tps65917",
  465. .data = &tps65917_data,
  466. },
  467. { },
  468. };
  469. MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);
  470. static int palmas_i2c_probe(struct i2c_client *i2c,
  471. const struct i2c_device_id *id)
  472. {
  473. struct palmas *palmas;
  474. struct palmas_platform_data *pdata;
  475. struct palmas_driver_data *driver_data;
  476. struct device_node *node = i2c->dev.of_node;
  477. int ret = 0, i;
  478. unsigned int reg, addr;
  479. int slave;
  480. const struct of_device_id *match;
  481. pdata = dev_get_platdata(&i2c->dev);
  482. if (node && !pdata) {
  483. pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
  484. if (!pdata)
  485. return -ENOMEM;
  486. palmas_dt_to_pdata(i2c, pdata);
  487. }
  488. if (!pdata)
  489. return -EINVAL;
  490. palmas = devm_kzalloc(&i2c->dev, sizeof(struct palmas), GFP_KERNEL);
  491. if (palmas == NULL)
  492. return -ENOMEM;
  493. i2c_set_clientdata(i2c, palmas);
  494. palmas->dev = &i2c->dev;
  495. palmas->irq = i2c->irq;
  496. match = of_match_device(of_palmas_match_tbl, &i2c->dev);
  497. if (!match)
  498. return -ENODATA;
  499. driver_data = (struct palmas_driver_data *)match->data;
  500. palmas->features = *driver_data->features;
  501. for (i = 0; i < PALMAS_NUM_CLIENTS; i++) {
  502. if (i == 0)
  503. palmas->i2c_clients[i] = i2c;
  504. else {
  505. palmas->i2c_clients[i] =
  506. i2c_new_dummy(i2c->adapter,
  507. i2c->addr + i);
  508. if (!palmas->i2c_clients[i]) {
  509. dev_err(palmas->dev,
  510. "can't attach client %d\n", i);
  511. ret = -ENOMEM;
  512. goto err_i2c;
  513. }
  514. palmas->i2c_clients[i]->dev.of_node = of_node_get(node);
  515. }
  516. palmas->regmap[i] = devm_regmap_init_i2c(palmas->i2c_clients[i],
  517. &palmas_regmap_config[i]);
  518. if (IS_ERR(palmas->regmap[i])) {
  519. ret = PTR_ERR(palmas->regmap[i]);
  520. dev_err(palmas->dev,
  521. "Failed to allocate regmap %d, err: %d\n",
  522. i, ret);
  523. goto err_i2c;
  524. }
  525. }
  526. if (!palmas->irq) {
  527. dev_warn(palmas->dev, "IRQ missing: skipping irq request\n");
  528. goto no_irq;
  529. }
  530. /* Change interrupt line output polarity */
  531. if (pdata->irq_flags & IRQ_TYPE_LEVEL_HIGH)
  532. reg = PALMAS_POLARITY_CTRL_INT_POLARITY;
  533. else
  534. reg = 0;
  535. ret = palmas_update_bits(palmas, PALMAS_PU_PD_OD_BASE,
  536. PALMAS_POLARITY_CTRL, PALMAS_POLARITY_CTRL_INT_POLARITY,
  537. reg);
  538. if (ret < 0) {
  539. dev_err(palmas->dev, "POLARITY_CTRL updat failed: %d\n", ret);
  540. goto err_i2c;
  541. }
  542. /* Change IRQ into clear on read mode for efficiency */
  543. slave = PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE);
  544. addr = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, PALMAS_INT_CTRL);
  545. reg = PALMAS_INT_CTRL_INT_CLEAR;
  546. regmap_write(palmas->regmap[slave], addr, reg);
  547. ret = regmap_add_irq_chip(palmas->regmap[slave], palmas->irq,
  548. IRQF_ONESHOT | pdata->irq_flags, 0,
  549. driver_data->irq_chip, &palmas->irq_data);
  550. if (ret < 0)
  551. goto err_i2c;
  552. no_irq:
  553. slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
  554. addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
  555. PALMAS_PRIMARY_SECONDARY_PAD1);
  556. if (pdata->mux_from_pdata) {
  557. reg = pdata->pad1;
  558. ret = regmap_write(palmas->regmap[slave], addr, reg);
  559. if (ret)
  560. goto err_irq;
  561. } else {
  562. ret = regmap_read(palmas->regmap[slave], addr, &reg);
  563. if (ret)
  564. goto err_irq;
  565. }
  566. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0))
  567. palmas->gpio_muxed |= PALMAS_GPIO_0_MUXED;
  568. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK))
  569. palmas->gpio_muxed |= PALMAS_GPIO_1_MUXED;
  570. else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
  571. (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
  572. palmas->led_muxed |= PALMAS_LED1_MUXED;
  573. else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
  574. (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
  575. palmas->pwm_muxed |= PALMAS_PWM1_MUXED;
  576. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK))
  577. palmas->gpio_muxed |= PALMAS_GPIO_2_MUXED;
  578. else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
  579. (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
  580. palmas->led_muxed |= PALMAS_LED2_MUXED;
  581. else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
  582. (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
  583. palmas->pwm_muxed |= PALMAS_PWM2_MUXED;
  584. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3))
  585. palmas->gpio_muxed |= PALMAS_GPIO_3_MUXED;
  586. addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
  587. PALMAS_PRIMARY_SECONDARY_PAD2);
  588. if (pdata->mux_from_pdata) {
  589. reg = pdata->pad2;
  590. ret = regmap_write(palmas->regmap[slave], addr, reg);
  591. if (ret)
  592. goto err_irq;
  593. } else {
  594. ret = regmap_read(palmas->regmap[slave], addr, &reg);
  595. if (ret)
  596. goto err_irq;
  597. }
  598. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4))
  599. palmas->gpio_muxed |= PALMAS_GPIO_4_MUXED;
  600. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK))
  601. palmas->gpio_muxed |= PALMAS_GPIO_5_MUXED;
  602. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6))
  603. palmas->gpio_muxed |= PALMAS_GPIO_6_MUXED;
  604. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK))
  605. palmas->gpio_muxed |= PALMAS_GPIO_7_MUXED;
  606. dev_info(palmas->dev, "Muxing GPIO %x, PWM %x, LED %x\n",
  607. palmas->gpio_muxed, palmas->pwm_muxed,
  608. palmas->led_muxed);
  609. reg = pdata->power_ctrl;
  610. slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
  611. addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_POWER_CTRL);
  612. ret = regmap_write(palmas->regmap[slave], addr, reg);
  613. if (ret)
  614. goto err_irq;
  615. /*
  616. * If we are probing with DT do this the DT way and return here
  617. * otherwise continue and add devices using mfd helpers.
  618. */
  619. if (node) {
  620. ret = of_platform_populate(node, NULL, NULL, &i2c->dev);
  621. if (ret < 0) {
  622. goto err_irq;
  623. } else if (pdata->pm_off && !pm_power_off) {
  624. palmas_dev = palmas;
  625. pm_power_off = palmas_power_off;
  626. }
  627. }
  628. return ret;
  629. err_irq:
  630. regmap_del_irq_chip(palmas->irq, palmas->irq_data);
  631. err_i2c:
  632. for (i = 1; i < PALMAS_NUM_CLIENTS; i++) {
  633. if (palmas->i2c_clients[i])
  634. i2c_unregister_device(palmas->i2c_clients[i]);
  635. }
  636. return ret;
  637. }
  638. static int palmas_i2c_remove(struct i2c_client *i2c)
  639. {
  640. struct palmas *palmas = i2c_get_clientdata(i2c);
  641. int i;
  642. regmap_del_irq_chip(palmas->irq, palmas->irq_data);
  643. for (i = 1; i < PALMAS_NUM_CLIENTS; i++) {
  644. if (palmas->i2c_clients[i])
  645. i2c_unregister_device(palmas->i2c_clients[i]);
  646. }
  647. if (palmas == palmas_dev) {
  648. pm_power_off = NULL;
  649. palmas_dev = NULL;
  650. }
  651. return 0;
  652. }
  653. static const struct i2c_device_id palmas_i2c_id[] = {
  654. { "palmas", },
  655. { "twl6035", },
  656. { "twl6037", },
  657. { "tps65913", },
  658. { /* end */ }
  659. };
  660. MODULE_DEVICE_TABLE(i2c, palmas_i2c_id);
  661. static struct i2c_driver palmas_i2c_driver = {
  662. .driver = {
  663. .name = "palmas",
  664. .of_match_table = of_palmas_match_tbl,
  665. },
  666. .probe = palmas_i2c_probe,
  667. .remove = palmas_i2c_remove,
  668. .id_table = palmas_i2c_id,
  669. };
  670. static int __init palmas_i2c_init(void)
  671. {
  672. return i2c_add_driver(&palmas_i2c_driver);
  673. }
  674. /* init early so consumer devices can complete system boot */
  675. subsys_initcall(palmas_i2c_init);
  676. static void __exit palmas_i2c_exit(void)
  677. {
  678. i2c_del_driver(&palmas_i2c_driver);
  679. }
  680. module_exit(palmas_i2c_exit);
  681. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  682. MODULE_DESCRIPTION("Palmas chip family multi-function driver");
  683. MODULE_LICENSE("GPL");