max8907.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * max8907.c - mfd driver for MAX8907
  3. *
  4. * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com>
  5. * Copyright (C) 2010-2012, NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/i2c.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/mfd/core.h>
  17. #include <linux/mfd/max8907.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. static const struct mfd_cell max8907_cells[] = {
  24. { .name = "max8907-regulator", },
  25. { .name = "max8907-rtc", },
  26. };
  27. static bool max8907_gen_is_volatile_reg(struct device *dev, unsigned int reg)
  28. {
  29. switch (reg) {
  30. case MAX8907_REG_ON_OFF_IRQ1:
  31. case MAX8907_REG_ON_OFF_STAT:
  32. case MAX8907_REG_ON_OFF_IRQ2:
  33. case MAX8907_REG_CHG_IRQ1:
  34. case MAX8907_REG_CHG_IRQ2:
  35. case MAX8907_REG_CHG_STAT:
  36. return true;
  37. default:
  38. return false;
  39. }
  40. }
  41. static bool max8907_gen_is_precious_reg(struct device *dev, unsigned int reg)
  42. {
  43. switch (reg) {
  44. case MAX8907_REG_ON_OFF_IRQ1:
  45. case MAX8907_REG_ON_OFF_IRQ2:
  46. case MAX8907_REG_CHG_IRQ1:
  47. case MAX8907_REG_CHG_IRQ2:
  48. return true;
  49. default:
  50. return false;
  51. }
  52. }
  53. static bool max8907_gen_is_writeable_reg(struct device *dev, unsigned int reg)
  54. {
  55. return !max8907_gen_is_volatile_reg(dev, reg);
  56. }
  57. static const struct regmap_config max8907_regmap_gen_config = {
  58. .reg_bits = 8,
  59. .val_bits = 8,
  60. .volatile_reg = max8907_gen_is_volatile_reg,
  61. .precious_reg = max8907_gen_is_precious_reg,
  62. .writeable_reg = max8907_gen_is_writeable_reg,
  63. .max_register = MAX8907_REG_LDO20VOUT,
  64. .cache_type = REGCACHE_RBTREE,
  65. };
  66. static bool max8907_rtc_is_volatile_reg(struct device *dev, unsigned int reg)
  67. {
  68. if (reg <= MAX8907_REG_RTC_YEAR2)
  69. return true;
  70. switch (reg) {
  71. case MAX8907_REG_RTC_STATUS:
  72. case MAX8907_REG_RTC_IRQ:
  73. return true;
  74. default:
  75. return false;
  76. }
  77. }
  78. static bool max8907_rtc_is_precious_reg(struct device *dev, unsigned int reg)
  79. {
  80. switch (reg) {
  81. case MAX8907_REG_RTC_IRQ:
  82. return true;
  83. default:
  84. return false;
  85. }
  86. }
  87. static bool max8907_rtc_is_writeable_reg(struct device *dev, unsigned int reg)
  88. {
  89. switch (reg) {
  90. case MAX8907_REG_RTC_STATUS:
  91. case MAX8907_REG_RTC_IRQ:
  92. return false;
  93. default:
  94. return true;
  95. }
  96. }
  97. static const struct regmap_config max8907_regmap_rtc_config = {
  98. .reg_bits = 8,
  99. .val_bits = 8,
  100. .volatile_reg = max8907_rtc_is_volatile_reg,
  101. .precious_reg = max8907_rtc_is_precious_reg,
  102. .writeable_reg = max8907_rtc_is_writeable_reg,
  103. .max_register = MAX8907_REG_MPL_CNTL,
  104. .cache_type = REGCACHE_RBTREE,
  105. };
  106. static const struct regmap_irq max8907_chg_irqs[] = {
  107. { .reg_offset = 0, .mask = 1 << 0, },
  108. { .reg_offset = 0, .mask = 1 << 1, },
  109. { .reg_offset = 0, .mask = 1 << 2, },
  110. { .reg_offset = 1, .mask = 1 << 0, },
  111. { .reg_offset = 1, .mask = 1 << 1, },
  112. { .reg_offset = 1, .mask = 1 << 2, },
  113. { .reg_offset = 1, .mask = 1 << 3, },
  114. { .reg_offset = 1, .mask = 1 << 4, },
  115. { .reg_offset = 1, .mask = 1 << 5, },
  116. { .reg_offset = 1, .mask = 1 << 6, },
  117. { .reg_offset = 1, .mask = 1 << 7, },
  118. };
  119. static const struct regmap_irq_chip max8907_chg_irq_chip = {
  120. .name = "max8907 chg",
  121. .status_base = MAX8907_REG_CHG_IRQ1,
  122. .mask_base = MAX8907_REG_CHG_IRQ1_MASK,
  123. .wake_base = MAX8907_REG_CHG_IRQ1_MASK,
  124. .irq_reg_stride = MAX8907_REG_CHG_IRQ2 - MAX8907_REG_CHG_IRQ1,
  125. .num_regs = 2,
  126. .irqs = max8907_chg_irqs,
  127. .num_irqs = ARRAY_SIZE(max8907_chg_irqs),
  128. };
  129. static const struct regmap_irq max8907_on_off_irqs[] = {
  130. { .reg_offset = 0, .mask = 1 << 0, },
  131. { .reg_offset = 0, .mask = 1 << 1, },
  132. { .reg_offset = 0, .mask = 1 << 2, },
  133. { .reg_offset = 0, .mask = 1 << 3, },
  134. { .reg_offset = 0, .mask = 1 << 4, },
  135. { .reg_offset = 0, .mask = 1 << 5, },
  136. { .reg_offset = 0, .mask = 1 << 6, },
  137. { .reg_offset = 0, .mask = 1 << 7, },
  138. { .reg_offset = 1, .mask = 1 << 0, },
  139. { .reg_offset = 1, .mask = 1 << 1, },
  140. };
  141. static const struct regmap_irq_chip max8907_on_off_irq_chip = {
  142. .name = "max8907 on_off",
  143. .status_base = MAX8907_REG_ON_OFF_IRQ1,
  144. .mask_base = MAX8907_REG_ON_OFF_IRQ1_MASK,
  145. .irq_reg_stride = MAX8907_REG_ON_OFF_IRQ2 - MAX8907_REG_ON_OFF_IRQ1,
  146. .num_regs = 2,
  147. .irqs = max8907_on_off_irqs,
  148. .num_irqs = ARRAY_SIZE(max8907_on_off_irqs),
  149. };
  150. static const struct regmap_irq max8907_rtc_irqs[] = {
  151. { .reg_offset = 0, .mask = 1 << 2, },
  152. { .reg_offset = 0, .mask = 1 << 3, },
  153. };
  154. static const struct regmap_irq_chip max8907_rtc_irq_chip = {
  155. .name = "max8907 rtc",
  156. .status_base = MAX8907_REG_RTC_IRQ,
  157. .mask_base = MAX8907_REG_RTC_IRQ_MASK,
  158. .num_regs = 1,
  159. .irqs = max8907_rtc_irqs,
  160. .num_irqs = ARRAY_SIZE(max8907_rtc_irqs),
  161. };
  162. static struct max8907 *max8907_pm_off;
  163. static void max8907_power_off(void)
  164. {
  165. regmap_update_bits(max8907_pm_off->regmap_gen, MAX8907_REG_RESET_CNFG,
  166. MAX8907_MASK_POWER_OFF, MAX8907_MASK_POWER_OFF);
  167. }
  168. static int max8907_i2c_probe(struct i2c_client *i2c,
  169. const struct i2c_device_id *id)
  170. {
  171. struct max8907 *max8907;
  172. int ret;
  173. struct max8907_platform_data *pdata = dev_get_platdata(&i2c->dev);
  174. bool pm_off = false;
  175. if (pdata)
  176. pm_off = pdata->pm_off;
  177. else if (i2c->dev.of_node)
  178. pm_off = of_property_read_bool(i2c->dev.of_node,
  179. "maxim,system-power-controller");
  180. max8907 = devm_kzalloc(&i2c->dev, sizeof(struct max8907), GFP_KERNEL);
  181. if (!max8907) {
  182. ret = -ENOMEM;
  183. goto err_alloc_drvdata;
  184. }
  185. max8907->dev = &i2c->dev;
  186. dev_set_drvdata(max8907->dev, max8907);
  187. max8907->i2c_gen = i2c;
  188. i2c_set_clientdata(i2c, max8907);
  189. max8907->regmap_gen = devm_regmap_init_i2c(i2c,
  190. &max8907_regmap_gen_config);
  191. if (IS_ERR(max8907->regmap_gen)) {
  192. ret = PTR_ERR(max8907->regmap_gen);
  193. dev_err(&i2c->dev, "gen regmap init failed: %d\n", ret);
  194. goto err_regmap_gen;
  195. }
  196. max8907->i2c_rtc = i2c_new_dummy(i2c->adapter, MAX8907_RTC_I2C_ADDR);
  197. if (!max8907->i2c_rtc) {
  198. ret = -ENOMEM;
  199. goto err_dummy_rtc;
  200. }
  201. i2c_set_clientdata(max8907->i2c_rtc, max8907);
  202. max8907->regmap_rtc = devm_regmap_init_i2c(max8907->i2c_rtc,
  203. &max8907_regmap_rtc_config);
  204. if (IS_ERR(max8907->regmap_rtc)) {
  205. ret = PTR_ERR(max8907->regmap_rtc);
  206. dev_err(&i2c->dev, "rtc regmap init failed: %d\n", ret);
  207. goto err_regmap_rtc;
  208. }
  209. irq_set_status_flags(max8907->i2c_gen->irq, IRQ_NOAUTOEN);
  210. ret = regmap_add_irq_chip(max8907->regmap_gen, max8907->i2c_gen->irq,
  211. IRQF_ONESHOT | IRQF_SHARED, -1,
  212. &max8907_chg_irq_chip,
  213. &max8907->irqc_chg);
  214. if (ret != 0) {
  215. dev_err(&i2c->dev, "failed to add chg irq chip: %d\n", ret);
  216. goto err_irqc_chg;
  217. }
  218. ret = regmap_add_irq_chip(max8907->regmap_gen, max8907->i2c_gen->irq,
  219. IRQF_ONESHOT | IRQF_SHARED, -1,
  220. &max8907_on_off_irq_chip,
  221. &max8907->irqc_on_off);
  222. if (ret != 0) {
  223. dev_err(&i2c->dev, "failed to add on off irq chip: %d\n", ret);
  224. goto err_irqc_on_off;
  225. }
  226. ret = regmap_add_irq_chip(max8907->regmap_rtc, max8907->i2c_gen->irq,
  227. IRQF_ONESHOT | IRQF_SHARED, -1,
  228. &max8907_rtc_irq_chip,
  229. &max8907->irqc_rtc);
  230. if (ret != 0) {
  231. dev_err(&i2c->dev, "failed to add rtc irq chip: %d\n", ret);
  232. goto err_irqc_rtc;
  233. }
  234. enable_irq(max8907->i2c_gen->irq);
  235. ret = mfd_add_devices(max8907->dev, -1, max8907_cells,
  236. ARRAY_SIZE(max8907_cells), NULL, 0, NULL);
  237. if (ret != 0) {
  238. dev_err(&i2c->dev, "failed to add MFD devices %d\n", ret);
  239. goto err_add_devices;
  240. }
  241. if (pm_off && !pm_power_off) {
  242. max8907_pm_off = max8907;
  243. pm_power_off = max8907_power_off;
  244. }
  245. return 0;
  246. err_add_devices:
  247. regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_rtc);
  248. err_irqc_rtc:
  249. regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_on_off);
  250. err_irqc_on_off:
  251. regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_chg);
  252. err_irqc_chg:
  253. err_regmap_rtc:
  254. i2c_unregister_device(max8907->i2c_rtc);
  255. err_dummy_rtc:
  256. err_regmap_gen:
  257. err_alloc_drvdata:
  258. return ret;
  259. }
  260. static int max8907_i2c_remove(struct i2c_client *i2c)
  261. {
  262. struct max8907 *max8907 = i2c_get_clientdata(i2c);
  263. mfd_remove_devices(max8907->dev);
  264. regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_rtc);
  265. regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_on_off);
  266. regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_chg);
  267. i2c_unregister_device(max8907->i2c_rtc);
  268. return 0;
  269. }
  270. #ifdef CONFIG_OF
  271. static const struct of_device_id max8907_of_match[] = {
  272. { .compatible = "maxim,max8907" },
  273. { },
  274. };
  275. MODULE_DEVICE_TABLE(of, max8907_of_match);
  276. #endif
  277. static const struct i2c_device_id max8907_i2c_id[] = {
  278. {"max8907", 0},
  279. {}
  280. };
  281. MODULE_DEVICE_TABLE(i2c, max8907_i2c_id);
  282. static struct i2c_driver max8907_i2c_driver = {
  283. .driver = {
  284. .name = "max8907",
  285. .of_match_table = of_match_ptr(max8907_of_match),
  286. },
  287. .probe = max8907_i2c_probe,
  288. .remove = max8907_i2c_remove,
  289. .id_table = max8907_i2c_id,
  290. };
  291. static int __init max8907_i2c_init(void)
  292. {
  293. int ret = -ENODEV;
  294. ret = i2c_add_driver(&max8907_i2c_driver);
  295. if (ret != 0)
  296. pr_err("Failed to register I2C driver: %d\n", ret);
  297. return ret;
  298. }
  299. subsys_initcall(max8907_i2c_init);
  300. static void __exit max8907_i2c_exit(void)
  301. {
  302. i2c_del_driver(&max8907_i2c_driver);
  303. }
  304. module_exit(max8907_i2c_exit);
  305. MODULE_DESCRIPTION("MAX8907 multi-function core driver");
  306. MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>");
  307. MODULE_LICENSE("GPL v2");