tps6586x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Core driver for TI TPS6586x PMIC family
  3. *
  4. * Copyright (c) 2010 CompuLab Ltd.
  5. * Mike Rapoport <mike@compulab.co.il>
  6. *
  7. * Based on da903x.c.
  8. * Copyright (C) 2008 Compulab, Ltd.
  9. * Mike Rapoport <mike@compulab.co.il>
  10. * Copyright (C) 2006-2008 Marvell International Ltd.
  11. * Eric Miao <eric.miao@marvell.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/slab.h>
  23. #include <linux/err.h>
  24. #include <linux/i2c.h>
  25. #include <linux/regmap.h>
  26. #include <linux/mfd/core.h>
  27. #include <linux/mfd/tps6586x.h>
  28. #define TPS6586X_SUPPLYENE 0x14
  29. #define EXITSLREQ_BIT BIT(1)
  30. #define SLEEP_MODE_BIT BIT(3)
  31. /* interrupt control registers */
  32. #define TPS6586X_INT_ACK1 0xb5
  33. #define TPS6586X_INT_ACK2 0xb6
  34. #define TPS6586X_INT_ACK3 0xb7
  35. #define TPS6586X_INT_ACK4 0xb8
  36. /* interrupt mask registers */
  37. #define TPS6586X_INT_MASK1 0xb0
  38. #define TPS6586X_INT_MASK2 0xb1
  39. #define TPS6586X_INT_MASK3 0xb2
  40. #define TPS6586X_INT_MASK4 0xb3
  41. #define TPS6586X_INT_MASK5 0xb4
  42. /* device id */
  43. #define TPS6586X_VERSIONCRC 0xcd
  44. /* Maximum register */
  45. #define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1)
  46. struct tps6586x_irq_data {
  47. u8 mask_reg;
  48. u8 mask_mask;
  49. };
  50. #define TPS6586X_IRQ(_reg, _mask) \
  51. { \
  52. .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
  53. .mask_mask = (_mask), \
  54. }
  55. static const struct tps6586x_irq_data tps6586x_irqs[] = {
  56. [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
  57. [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
  58. [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
  59. [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
  60. [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
  61. [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
  62. [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
  63. [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
  64. [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
  65. [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
  66. [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
  67. [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
  68. [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
  69. [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
  70. [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
  71. [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
  72. [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
  73. [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
  74. [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
  75. [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
  76. [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
  77. [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
  78. [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
  79. [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
  80. [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
  81. [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
  82. [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
  83. };
  84. static struct mfd_cell tps6586x_cell[] = {
  85. {
  86. .name = "tps6586x-gpio",
  87. },
  88. {
  89. .name = "tps6586x-pmic",
  90. },
  91. {
  92. .name = "tps6586x-rtc",
  93. },
  94. {
  95. .name = "tps6586x-onkey",
  96. },
  97. };
  98. struct tps6586x {
  99. struct device *dev;
  100. struct i2c_client *client;
  101. struct regmap *regmap;
  102. struct irq_chip irq_chip;
  103. struct mutex irq_lock;
  104. int irq_base;
  105. u32 irq_en;
  106. u8 mask_reg[5];
  107. };
  108. static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
  109. {
  110. return i2c_get_clientdata(to_i2c_client(dev));
  111. }
  112. int tps6586x_write(struct device *dev, int reg, uint8_t val)
  113. {
  114. struct tps6586x *tps6586x = dev_to_tps6586x(dev);
  115. return regmap_write(tps6586x->regmap, reg, val);
  116. }
  117. EXPORT_SYMBOL_GPL(tps6586x_write);
  118. int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
  119. {
  120. struct tps6586x *tps6586x = dev_to_tps6586x(dev);
  121. return regmap_bulk_write(tps6586x->regmap, reg, val, len);
  122. }
  123. EXPORT_SYMBOL_GPL(tps6586x_writes);
  124. int tps6586x_read(struct device *dev, int reg, uint8_t *val)
  125. {
  126. struct tps6586x *tps6586x = dev_to_tps6586x(dev);
  127. unsigned int rval;
  128. int ret;
  129. ret = regmap_read(tps6586x->regmap, reg, &rval);
  130. if (!ret)
  131. *val = rval;
  132. return ret;
  133. }
  134. EXPORT_SYMBOL_GPL(tps6586x_read);
  135. int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
  136. {
  137. struct tps6586x *tps6586x = dev_to_tps6586x(dev);
  138. return regmap_bulk_read(tps6586x->regmap, reg, val, len);
  139. }
  140. EXPORT_SYMBOL_GPL(tps6586x_reads);
  141. int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
  142. {
  143. struct tps6586x *tps6586x = dev_to_tps6586x(dev);
  144. return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
  145. }
  146. EXPORT_SYMBOL_GPL(tps6586x_set_bits);
  147. int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
  148. {
  149. struct tps6586x *tps6586x = dev_to_tps6586x(dev);
  150. return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
  151. }
  152. EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
  153. int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
  154. {
  155. struct tps6586x *tps6586x = dev_to_tps6586x(dev);
  156. return regmap_update_bits(tps6586x->regmap, reg, mask, val);
  157. }
  158. EXPORT_SYMBOL_GPL(tps6586x_update);
  159. static int __remove_subdev(struct device *dev, void *unused)
  160. {
  161. platform_device_unregister(to_platform_device(dev));
  162. return 0;
  163. }
  164. static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
  165. {
  166. return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
  167. }
  168. static void tps6586x_irq_lock(struct irq_data *data)
  169. {
  170. struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
  171. mutex_lock(&tps6586x->irq_lock);
  172. }
  173. static void tps6586x_irq_enable(struct irq_data *irq_data)
  174. {
  175. struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
  176. unsigned int __irq = irq_data->irq - tps6586x->irq_base;
  177. const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
  178. tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
  179. tps6586x->irq_en |= (1 << __irq);
  180. }
  181. static void tps6586x_irq_disable(struct irq_data *irq_data)
  182. {
  183. struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
  184. unsigned int __irq = irq_data->irq - tps6586x->irq_base;
  185. const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
  186. tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
  187. tps6586x->irq_en &= ~(1 << __irq);
  188. }
  189. static void tps6586x_irq_sync_unlock(struct irq_data *data)
  190. {
  191. struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
  192. int i;
  193. for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
  194. int ret;
  195. ret = tps6586x_write(tps6586x->dev,
  196. TPS6586X_INT_MASK1 + i,
  197. tps6586x->mask_reg[i]);
  198. WARN_ON(ret);
  199. }
  200. mutex_unlock(&tps6586x->irq_lock);
  201. }
  202. static irqreturn_t tps6586x_irq(int irq, void *data)
  203. {
  204. struct tps6586x *tps6586x = data;
  205. u32 acks;
  206. int ret = 0;
  207. ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
  208. sizeof(acks), (uint8_t *)&acks);
  209. if (ret < 0) {
  210. dev_err(tps6586x->dev, "failed to read interrupt status\n");
  211. return IRQ_NONE;
  212. }
  213. acks = le32_to_cpu(acks);
  214. while (acks) {
  215. int i = __ffs(acks);
  216. if (tps6586x->irq_en & (1 << i))
  217. handle_nested_irq(tps6586x->irq_base + i);
  218. acks &= ~(1 << i);
  219. }
  220. return IRQ_HANDLED;
  221. }
  222. static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
  223. int irq_base)
  224. {
  225. int i, ret;
  226. u8 tmp[4];
  227. if (!irq_base) {
  228. dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
  229. return -EINVAL;
  230. }
  231. mutex_init(&tps6586x->irq_lock);
  232. for (i = 0; i < 5; i++) {
  233. tps6586x->mask_reg[i] = 0xff;
  234. tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
  235. }
  236. tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
  237. tps6586x->irq_base = irq_base;
  238. tps6586x->irq_chip.name = "tps6586x";
  239. tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
  240. tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
  241. tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
  242. tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
  243. for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
  244. int __irq = i + tps6586x->irq_base;
  245. irq_set_chip_data(__irq, tps6586x);
  246. irq_set_chip_and_handler(__irq, &tps6586x->irq_chip,
  247. handle_simple_irq);
  248. irq_set_nested_thread(__irq, 1);
  249. #ifdef CONFIG_ARM
  250. set_irq_flags(__irq, IRQF_VALID);
  251. #endif
  252. }
  253. ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
  254. "tps6586x", tps6586x);
  255. if (!ret) {
  256. device_init_wakeup(tps6586x->dev, 1);
  257. enable_irq_wake(irq);
  258. }
  259. return ret;
  260. }
  261. static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
  262. struct tps6586x_platform_data *pdata)
  263. {
  264. struct tps6586x_subdev_info *subdev;
  265. struct platform_device *pdev;
  266. int i, ret = 0;
  267. for (i = 0; i < pdata->num_subdevs; i++) {
  268. subdev = &pdata->subdevs[i];
  269. pdev = platform_device_alloc(subdev->name, subdev->id);
  270. if (!pdev) {
  271. ret = -ENOMEM;
  272. goto failed;
  273. }
  274. pdev->dev.parent = tps6586x->dev;
  275. pdev->dev.platform_data = subdev->platform_data;
  276. pdev->dev.of_node = subdev->of_node;
  277. ret = platform_device_add(pdev);
  278. if (ret) {
  279. platform_device_put(pdev);
  280. goto failed;
  281. }
  282. }
  283. return 0;
  284. failed:
  285. tps6586x_remove_subdevs(tps6586x);
  286. return ret;
  287. }
  288. #ifdef CONFIG_OF
  289. static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
  290. {
  291. struct device_node *np = client->dev.of_node;
  292. struct tps6586x_platform_data *pdata;
  293. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  294. if (!pdata) {
  295. dev_err(&client->dev, "Memory allocation failed\n");
  296. return NULL;
  297. }
  298. pdata->num_subdevs = 0;
  299. pdata->subdevs = NULL;
  300. pdata->gpio_base = -1;
  301. pdata->irq_base = -1;
  302. pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
  303. return pdata;
  304. }
  305. static struct of_device_id tps6586x_of_match[] = {
  306. { .compatible = "ti,tps6586x", },
  307. { },
  308. };
  309. #else
  310. static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
  311. {
  312. return NULL;
  313. }
  314. #endif
  315. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  316. {
  317. /* Cache all interrupt mask register */
  318. if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
  319. return false;
  320. return true;
  321. }
  322. static const struct regmap_config tps6586x_regmap_config = {
  323. .reg_bits = 8,
  324. .val_bits = 8,
  325. .max_register = TPS6586X_MAX_REGISTER - 1,
  326. .volatile_reg = is_volatile_reg,
  327. .cache_type = REGCACHE_RBTREE,
  328. };
  329. static struct device *tps6586x_dev;
  330. static void tps6586x_power_off(void)
  331. {
  332. if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
  333. return;
  334. tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
  335. }
  336. static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
  337. const struct i2c_device_id *id)
  338. {
  339. struct tps6586x_platform_data *pdata = client->dev.platform_data;
  340. struct tps6586x *tps6586x;
  341. int ret;
  342. if (!pdata && client->dev.of_node)
  343. pdata = tps6586x_parse_dt(client);
  344. if (!pdata) {
  345. dev_err(&client->dev, "tps6586x requires platform data\n");
  346. return -ENOTSUPP;
  347. }
  348. ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
  349. if (ret < 0) {
  350. dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
  351. return -EIO;
  352. }
  353. dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
  354. tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
  355. if (tps6586x == NULL) {
  356. dev_err(&client->dev, "memory for tps6586x alloc failed\n");
  357. return -ENOMEM;
  358. }
  359. tps6586x->client = client;
  360. tps6586x->dev = &client->dev;
  361. i2c_set_clientdata(client, tps6586x);
  362. tps6586x->regmap = devm_regmap_init_i2c(client,
  363. &tps6586x_regmap_config);
  364. if (IS_ERR(tps6586x->regmap)) {
  365. ret = PTR_ERR(tps6586x->regmap);
  366. dev_err(&client->dev, "regmap init failed: %d\n", ret);
  367. return ret;
  368. }
  369. if (client->irq) {
  370. ret = tps6586x_irq_init(tps6586x, client->irq,
  371. pdata->irq_base);
  372. if (ret) {
  373. dev_err(&client->dev, "IRQ init failed: %d\n", ret);
  374. return ret;
  375. }
  376. }
  377. ret = mfd_add_devices(tps6586x->dev, -1,
  378. tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
  379. NULL, 0, NULL);
  380. if (ret < 0) {
  381. dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
  382. goto err_mfd_add;
  383. }
  384. ret = tps6586x_add_subdevs(tps6586x, pdata);
  385. if (ret) {
  386. dev_err(&client->dev, "add devices failed: %d\n", ret);
  387. goto err_add_devs;
  388. }
  389. if (pdata->pm_off && !pm_power_off) {
  390. tps6586x_dev = &client->dev;
  391. pm_power_off = tps6586x_power_off;
  392. }
  393. return 0;
  394. err_add_devs:
  395. mfd_remove_devices(tps6586x->dev);
  396. err_mfd_add:
  397. if (client->irq)
  398. free_irq(client->irq, tps6586x);
  399. return ret;
  400. }
  401. static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
  402. {
  403. struct tps6586x *tps6586x = i2c_get_clientdata(client);
  404. tps6586x_remove_subdevs(tps6586x);
  405. mfd_remove_devices(tps6586x->dev);
  406. if (client->irq)
  407. free_irq(client->irq, tps6586x);
  408. return 0;
  409. }
  410. static const struct i2c_device_id tps6586x_id_table[] = {
  411. { "tps6586x", 0 },
  412. { },
  413. };
  414. MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
  415. static struct i2c_driver tps6586x_driver = {
  416. .driver = {
  417. .name = "tps6586x",
  418. .owner = THIS_MODULE,
  419. .of_match_table = of_match_ptr(tps6586x_of_match),
  420. },
  421. .probe = tps6586x_i2c_probe,
  422. .remove = __devexit_p(tps6586x_i2c_remove),
  423. .id_table = tps6586x_id_table,
  424. };
  425. static int __init tps6586x_init(void)
  426. {
  427. return i2c_add_driver(&tps6586x_driver);
  428. }
  429. subsys_initcall(tps6586x_init);
  430. static void __exit tps6586x_exit(void)
  431. {
  432. i2c_del_driver(&tps6586x_driver);
  433. }
  434. module_exit(tps6586x_exit);
  435. MODULE_DESCRIPTION("TPS6586X core driver");
  436. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  437. MODULE_LICENSE("GPL");