palmas.c 18 KB

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