palmas.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. u8 powerhold_mask;
  412. struct device_node *np = palmas_dev->dev->of_node;
  413. if (of_property_read_bool(np, "ti,palmas-override-powerhold")) {
  414. addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
  415. PALMAS_PRIMARY_SECONDARY_PAD2);
  416. slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
  417. if (of_device_is_compatible(np, "ti,tps65917"))
  418. powerhold_mask =
  419. TPS65917_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK;
  420. else
  421. powerhold_mask =
  422. PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK;
  423. ret = regmap_update_bits(palmas_dev->regmap[slave], addr,
  424. powerhold_mask, 0);
  425. if (ret)
  426. dev_err(palmas_dev->dev,
  427. "Unable to write PRIMARY_SECONDARY_PAD2 %d\n",
  428. ret);
  429. }
  430. slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
  431. addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_DEV_CTRL);
  432. ret = regmap_update_bits(
  433. palmas_dev->regmap[slave],
  434. addr,
  435. PALMAS_DEV_CTRL_DEV_ON,
  436. 0);
  437. if (ret)
  438. pr_err("%s: Unable to write to DEV_CTRL_DEV_ON: %d\n",
  439. __func__, ret);
  440. }
  441. static unsigned int palmas_features = PALMAS_PMIC_FEATURE_SMPS10_BOOST;
  442. static unsigned int tps659038_features;
  443. struct palmas_driver_data {
  444. unsigned int *features;
  445. struct regmap_irq_chip *irq_chip;
  446. };
  447. static struct palmas_driver_data palmas_data = {
  448. .features = &palmas_features,
  449. .irq_chip = &palmas_irq_chip,
  450. };
  451. static struct palmas_driver_data tps659038_data = {
  452. .features = &tps659038_features,
  453. .irq_chip = &palmas_irq_chip,
  454. };
  455. static struct palmas_driver_data tps65917_data = {
  456. .features = &tps659038_features,
  457. .irq_chip = &tps65917_irq_chip,
  458. };
  459. static const struct of_device_id of_palmas_match_tbl[] = {
  460. {
  461. .compatible = "ti,palmas",
  462. .data = &palmas_data,
  463. },
  464. {
  465. .compatible = "ti,tps659038",
  466. .data = &tps659038_data,
  467. },
  468. {
  469. .compatible = "ti,tps65917",
  470. .data = &tps65917_data,
  471. },
  472. { },
  473. };
  474. MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);
  475. static int palmas_i2c_probe(struct i2c_client *i2c,
  476. const struct i2c_device_id *id)
  477. {
  478. struct palmas *palmas;
  479. struct palmas_platform_data *pdata;
  480. struct palmas_driver_data *driver_data;
  481. struct device_node *node = i2c->dev.of_node;
  482. int ret = 0, i;
  483. unsigned int reg, addr;
  484. int slave;
  485. const struct of_device_id *match;
  486. pdata = dev_get_platdata(&i2c->dev);
  487. if (node && !pdata) {
  488. pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
  489. if (!pdata)
  490. return -ENOMEM;
  491. palmas_dt_to_pdata(i2c, pdata);
  492. }
  493. if (!pdata)
  494. return -EINVAL;
  495. palmas = devm_kzalloc(&i2c->dev, sizeof(struct palmas), GFP_KERNEL);
  496. if (palmas == NULL)
  497. return -ENOMEM;
  498. i2c_set_clientdata(i2c, palmas);
  499. palmas->dev = &i2c->dev;
  500. palmas->irq = i2c->irq;
  501. match = of_match_device(of_palmas_match_tbl, &i2c->dev);
  502. if (!match)
  503. return -ENODATA;
  504. driver_data = (struct palmas_driver_data *)match->data;
  505. palmas->features = *driver_data->features;
  506. for (i = 0; i < PALMAS_NUM_CLIENTS; i++) {
  507. if (i == 0)
  508. palmas->i2c_clients[i] = i2c;
  509. else {
  510. palmas->i2c_clients[i] =
  511. i2c_new_dummy(i2c->adapter,
  512. i2c->addr + i);
  513. if (!palmas->i2c_clients[i]) {
  514. dev_err(palmas->dev,
  515. "can't attach client %d\n", i);
  516. ret = -ENOMEM;
  517. goto err_i2c;
  518. }
  519. palmas->i2c_clients[i]->dev.of_node = of_node_get(node);
  520. }
  521. palmas->regmap[i] = devm_regmap_init_i2c(palmas->i2c_clients[i],
  522. &palmas_regmap_config[i]);
  523. if (IS_ERR(palmas->regmap[i])) {
  524. ret = PTR_ERR(palmas->regmap[i]);
  525. dev_err(palmas->dev,
  526. "Failed to allocate regmap %d, err: %d\n",
  527. i, ret);
  528. goto err_i2c;
  529. }
  530. }
  531. if (!palmas->irq) {
  532. dev_warn(palmas->dev, "IRQ missing: skipping irq request\n");
  533. goto no_irq;
  534. }
  535. /* Change interrupt line output polarity */
  536. if (pdata->irq_flags & IRQ_TYPE_LEVEL_HIGH)
  537. reg = PALMAS_POLARITY_CTRL_INT_POLARITY;
  538. else
  539. reg = 0;
  540. ret = palmas_update_bits(palmas, PALMAS_PU_PD_OD_BASE,
  541. PALMAS_POLARITY_CTRL, PALMAS_POLARITY_CTRL_INT_POLARITY,
  542. reg);
  543. if (ret < 0) {
  544. dev_err(palmas->dev, "POLARITY_CTRL update failed: %d\n", ret);
  545. goto err_i2c;
  546. }
  547. /* Change IRQ into clear on read mode for efficiency */
  548. slave = PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE);
  549. addr = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, PALMAS_INT_CTRL);
  550. reg = PALMAS_INT_CTRL_INT_CLEAR;
  551. regmap_write(palmas->regmap[slave], addr, reg);
  552. ret = regmap_add_irq_chip(palmas->regmap[slave], palmas->irq,
  553. IRQF_ONESHOT | pdata->irq_flags, 0,
  554. driver_data->irq_chip, &palmas->irq_data);
  555. if (ret < 0)
  556. goto err_i2c;
  557. no_irq:
  558. slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
  559. addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
  560. PALMAS_PRIMARY_SECONDARY_PAD1);
  561. if (pdata->mux_from_pdata) {
  562. reg = pdata->pad1;
  563. ret = regmap_write(palmas->regmap[slave], addr, reg);
  564. if (ret)
  565. goto err_irq;
  566. } else {
  567. ret = regmap_read(palmas->regmap[slave], addr, &reg);
  568. if (ret)
  569. goto err_irq;
  570. }
  571. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0))
  572. palmas->gpio_muxed |= PALMAS_GPIO_0_MUXED;
  573. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK))
  574. palmas->gpio_muxed |= PALMAS_GPIO_1_MUXED;
  575. else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
  576. (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
  577. palmas->led_muxed |= PALMAS_LED1_MUXED;
  578. else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
  579. (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
  580. palmas->pwm_muxed |= PALMAS_PWM1_MUXED;
  581. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK))
  582. palmas->gpio_muxed |= PALMAS_GPIO_2_MUXED;
  583. else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
  584. (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
  585. palmas->led_muxed |= PALMAS_LED2_MUXED;
  586. else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
  587. (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
  588. palmas->pwm_muxed |= PALMAS_PWM2_MUXED;
  589. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3))
  590. palmas->gpio_muxed |= PALMAS_GPIO_3_MUXED;
  591. addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
  592. PALMAS_PRIMARY_SECONDARY_PAD2);
  593. if (pdata->mux_from_pdata) {
  594. reg = pdata->pad2;
  595. ret = regmap_write(palmas->regmap[slave], addr, reg);
  596. if (ret)
  597. goto err_irq;
  598. } else {
  599. ret = regmap_read(palmas->regmap[slave], addr, &reg);
  600. if (ret)
  601. goto err_irq;
  602. }
  603. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4))
  604. palmas->gpio_muxed |= PALMAS_GPIO_4_MUXED;
  605. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK))
  606. palmas->gpio_muxed |= PALMAS_GPIO_5_MUXED;
  607. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6))
  608. palmas->gpio_muxed |= PALMAS_GPIO_6_MUXED;
  609. if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK))
  610. palmas->gpio_muxed |= PALMAS_GPIO_7_MUXED;
  611. dev_info(palmas->dev, "Muxing GPIO %x, PWM %x, LED %x\n",
  612. palmas->gpio_muxed, palmas->pwm_muxed,
  613. palmas->led_muxed);
  614. reg = pdata->power_ctrl;
  615. slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
  616. addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_POWER_CTRL);
  617. ret = regmap_write(palmas->regmap[slave], addr, reg);
  618. if (ret)
  619. goto err_irq;
  620. /*
  621. * If we are probing with DT do this the DT way and return here
  622. * otherwise continue and add devices using mfd helpers.
  623. */
  624. if (node) {
  625. ret = devm_of_platform_populate(&i2c->dev);
  626. if (ret < 0) {
  627. goto err_irq;
  628. } else if (pdata->pm_off && !pm_power_off) {
  629. palmas_dev = palmas;
  630. pm_power_off = palmas_power_off;
  631. }
  632. }
  633. return ret;
  634. err_irq:
  635. regmap_del_irq_chip(palmas->irq, palmas->irq_data);
  636. err_i2c:
  637. for (i = 1; i < PALMAS_NUM_CLIENTS; i++) {
  638. if (palmas->i2c_clients[i])
  639. i2c_unregister_device(palmas->i2c_clients[i]);
  640. }
  641. return ret;
  642. }
  643. static int palmas_i2c_remove(struct i2c_client *i2c)
  644. {
  645. struct palmas *palmas = i2c_get_clientdata(i2c);
  646. int i;
  647. regmap_del_irq_chip(palmas->irq, palmas->irq_data);
  648. for (i = 1; i < PALMAS_NUM_CLIENTS; i++) {
  649. if (palmas->i2c_clients[i])
  650. i2c_unregister_device(palmas->i2c_clients[i]);
  651. }
  652. if (palmas == palmas_dev) {
  653. pm_power_off = NULL;
  654. palmas_dev = NULL;
  655. }
  656. return 0;
  657. }
  658. static const struct i2c_device_id palmas_i2c_id[] = {
  659. { "palmas", },
  660. { "twl6035", },
  661. { "twl6037", },
  662. { "tps65913", },
  663. { /* end */ }
  664. };
  665. MODULE_DEVICE_TABLE(i2c, palmas_i2c_id);
  666. static struct i2c_driver palmas_i2c_driver = {
  667. .driver = {
  668. .name = "palmas",
  669. .of_match_table = of_palmas_match_tbl,
  670. },
  671. .probe = palmas_i2c_probe,
  672. .remove = palmas_i2c_remove,
  673. .id_table = palmas_i2c_id,
  674. };
  675. static int __init palmas_i2c_init(void)
  676. {
  677. return i2c_add_driver(&palmas_i2c_driver);
  678. }
  679. /* init early so consumer devices can complete system boot */
  680. subsys_initcall(palmas_i2c_init);
  681. static void __exit palmas_i2c_exit(void)
  682. {
  683. i2c_del_driver(&palmas_i2c_driver);
  684. }
  685. module_exit(palmas_i2c_exit);
  686. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  687. MODULE_DESCRIPTION("Palmas chip family multi-function driver");
  688. MODULE_LICENSE("GPL");