retu-mfd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Retu/Tahvo MFD driver
  3. *
  4. * Copyright (C) 2004, 2005 Nokia Corporation
  5. *
  6. * Based on code written by Juha Yrjölä, David Weinehall and Mikko Ylinen.
  7. * Rewritten by Aaro Koskinen.
  8. *
  9. * This file is subject to the terms and conditions of the GNU General
  10. * Public License. See the file "COPYING" in the main directory of this
  11. * archive for more details.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/err.h>
  19. #include <linux/i2c.h>
  20. #include <linux/irq.h>
  21. #include <linux/slab.h>
  22. #include <linux/mutex.h>
  23. #include <linux/module.h>
  24. #include <linux/regmap.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/mfd/retu.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/moduleparam.h>
  29. /* Registers */
  30. #define RETU_REG_ASICR 0x00 /* ASIC ID and revision */
  31. #define RETU_REG_ASICR_VILMA (1 << 7) /* Bit indicating Vilma */
  32. #define RETU_REG_IDR 0x01 /* Interrupt ID */
  33. #define RETU_REG_IMR 0x02 /* Interrupt mask (Retu) */
  34. #define TAHVO_REG_IMR 0x03 /* Interrupt mask (Tahvo) */
  35. /* Interrupt sources */
  36. #define RETU_INT_PWR 0 /* Power button */
  37. struct retu_dev {
  38. struct regmap *regmap;
  39. struct device *dev;
  40. struct mutex mutex;
  41. struct regmap_irq_chip_data *irq_data;
  42. };
  43. static struct resource retu_pwrbutton_res[] = {
  44. {
  45. .name = "retu-pwrbutton",
  46. .start = RETU_INT_PWR,
  47. .end = RETU_INT_PWR,
  48. .flags = IORESOURCE_IRQ,
  49. },
  50. };
  51. static const struct mfd_cell retu_devs[] = {
  52. {
  53. .name = "retu-wdt"
  54. },
  55. {
  56. .name = "retu-pwrbutton",
  57. .resources = retu_pwrbutton_res,
  58. .num_resources = ARRAY_SIZE(retu_pwrbutton_res),
  59. }
  60. };
  61. static struct regmap_irq retu_irqs[] = {
  62. [RETU_INT_PWR] = {
  63. .mask = 1 << RETU_INT_PWR,
  64. }
  65. };
  66. static struct regmap_irq_chip retu_irq_chip = {
  67. .name = "RETU",
  68. .irqs = retu_irqs,
  69. .num_irqs = ARRAY_SIZE(retu_irqs),
  70. .num_regs = 1,
  71. .status_base = RETU_REG_IDR,
  72. .mask_base = RETU_REG_IMR,
  73. .ack_base = RETU_REG_IDR,
  74. };
  75. /* Retu device registered for the power off. */
  76. static struct retu_dev *retu_pm_power_off;
  77. static struct resource tahvo_usb_res[] = {
  78. {
  79. .name = "tahvo-usb",
  80. .start = TAHVO_INT_VBUS,
  81. .end = TAHVO_INT_VBUS,
  82. .flags = IORESOURCE_IRQ,
  83. },
  84. };
  85. static const struct mfd_cell tahvo_devs[] = {
  86. {
  87. .name = "tahvo-usb",
  88. .resources = tahvo_usb_res,
  89. .num_resources = ARRAY_SIZE(tahvo_usb_res),
  90. },
  91. };
  92. static struct regmap_irq tahvo_irqs[] = {
  93. [TAHVO_INT_VBUS] = {
  94. .mask = 1 << TAHVO_INT_VBUS,
  95. }
  96. };
  97. static struct regmap_irq_chip tahvo_irq_chip = {
  98. .name = "TAHVO",
  99. .irqs = tahvo_irqs,
  100. .num_irqs = ARRAY_SIZE(tahvo_irqs),
  101. .num_regs = 1,
  102. .status_base = RETU_REG_IDR,
  103. .mask_base = TAHVO_REG_IMR,
  104. .ack_base = RETU_REG_IDR,
  105. };
  106. static const struct retu_data {
  107. char *chip_name;
  108. char *companion_name;
  109. struct regmap_irq_chip *irq_chip;
  110. const struct mfd_cell *children;
  111. int nchildren;
  112. } retu_data[] = {
  113. [0] = {
  114. .chip_name = "Retu",
  115. .companion_name = "Vilma",
  116. .irq_chip = &retu_irq_chip,
  117. .children = retu_devs,
  118. .nchildren = ARRAY_SIZE(retu_devs),
  119. },
  120. [1] = {
  121. .chip_name = "Tahvo",
  122. .companion_name = "Betty",
  123. .irq_chip = &tahvo_irq_chip,
  124. .children = tahvo_devs,
  125. .nchildren = ARRAY_SIZE(tahvo_devs),
  126. }
  127. };
  128. int retu_read(struct retu_dev *rdev, u8 reg)
  129. {
  130. int ret;
  131. int value;
  132. mutex_lock(&rdev->mutex);
  133. ret = regmap_read(rdev->regmap, reg, &value);
  134. mutex_unlock(&rdev->mutex);
  135. return ret ? ret : value;
  136. }
  137. EXPORT_SYMBOL_GPL(retu_read);
  138. int retu_write(struct retu_dev *rdev, u8 reg, u16 data)
  139. {
  140. int ret;
  141. mutex_lock(&rdev->mutex);
  142. ret = regmap_write(rdev->regmap, reg, data);
  143. mutex_unlock(&rdev->mutex);
  144. return ret;
  145. }
  146. EXPORT_SYMBOL_GPL(retu_write);
  147. static void retu_power_off(void)
  148. {
  149. struct retu_dev *rdev = retu_pm_power_off;
  150. int reg;
  151. mutex_lock(&retu_pm_power_off->mutex);
  152. /* Ignore power button state */
  153. regmap_read(rdev->regmap, RETU_REG_CC1, &reg);
  154. regmap_write(rdev->regmap, RETU_REG_CC1, reg | 2);
  155. /* Expire watchdog immediately */
  156. regmap_write(rdev->regmap, RETU_REG_WATCHDOG, 0);
  157. /* Wait for poweroff */
  158. for (;;)
  159. cpu_relax();
  160. mutex_unlock(&retu_pm_power_off->mutex);
  161. }
  162. static int retu_regmap_read(void *context, const void *reg, size_t reg_size,
  163. void *val, size_t val_size)
  164. {
  165. int ret;
  166. struct device *dev = context;
  167. struct i2c_client *i2c = to_i2c_client(dev);
  168. BUG_ON(reg_size != 1 || val_size != 2);
  169. ret = i2c_smbus_read_word_data(i2c, *(u8 const *)reg);
  170. if (ret < 0)
  171. return ret;
  172. *(u16 *)val = ret;
  173. return 0;
  174. }
  175. static int retu_regmap_write(void *context, const void *data, size_t count)
  176. {
  177. u8 reg;
  178. u16 val;
  179. struct device *dev = context;
  180. struct i2c_client *i2c = to_i2c_client(dev);
  181. BUG_ON(count != sizeof(reg) + sizeof(val));
  182. memcpy(&reg, data, sizeof(reg));
  183. memcpy(&val, data + sizeof(reg), sizeof(val));
  184. return i2c_smbus_write_word_data(i2c, reg, val);
  185. }
  186. static struct regmap_bus retu_bus = {
  187. .read = retu_regmap_read,
  188. .write = retu_regmap_write,
  189. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  190. };
  191. static const struct regmap_config retu_config = {
  192. .reg_bits = 8,
  193. .val_bits = 16,
  194. };
  195. static int retu_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  196. {
  197. struct retu_data const *rdat;
  198. struct retu_dev *rdev;
  199. int ret;
  200. if (i2c->addr > ARRAY_SIZE(retu_data))
  201. return -ENODEV;
  202. rdat = &retu_data[i2c->addr - 1];
  203. rdev = devm_kzalloc(&i2c->dev, sizeof(*rdev), GFP_KERNEL);
  204. if (rdev == NULL)
  205. return -ENOMEM;
  206. i2c_set_clientdata(i2c, rdev);
  207. rdev->dev = &i2c->dev;
  208. mutex_init(&rdev->mutex);
  209. rdev->regmap = devm_regmap_init(&i2c->dev, &retu_bus, &i2c->dev,
  210. &retu_config);
  211. if (IS_ERR(rdev->regmap))
  212. return PTR_ERR(rdev->regmap);
  213. ret = retu_read(rdev, RETU_REG_ASICR);
  214. if (ret < 0) {
  215. dev_err(rdev->dev, "could not read %s revision: %d\n",
  216. rdat->chip_name, ret);
  217. return ret;
  218. }
  219. dev_info(rdev->dev, "%s%s%s v%d.%d found\n", rdat->chip_name,
  220. (ret & RETU_REG_ASICR_VILMA) ? " & " : "",
  221. (ret & RETU_REG_ASICR_VILMA) ? rdat->companion_name : "",
  222. (ret >> 4) & 0x7, ret & 0xf);
  223. /* Mask all interrupts. */
  224. ret = retu_write(rdev, rdat->irq_chip->mask_base, 0xffff);
  225. if (ret < 0)
  226. return ret;
  227. ret = regmap_add_irq_chip(rdev->regmap, i2c->irq, IRQF_ONESHOT, -1,
  228. rdat->irq_chip, &rdev->irq_data);
  229. if (ret < 0)
  230. return ret;
  231. ret = mfd_add_devices(rdev->dev, -1, rdat->children, rdat->nchildren,
  232. NULL, regmap_irq_chip_get_base(rdev->irq_data),
  233. NULL);
  234. if (ret < 0) {
  235. regmap_del_irq_chip(i2c->irq, rdev->irq_data);
  236. return ret;
  237. }
  238. if (i2c->addr == 1 && !pm_power_off) {
  239. retu_pm_power_off = rdev;
  240. pm_power_off = retu_power_off;
  241. }
  242. return 0;
  243. }
  244. static int retu_remove(struct i2c_client *i2c)
  245. {
  246. struct retu_dev *rdev = i2c_get_clientdata(i2c);
  247. if (retu_pm_power_off == rdev) {
  248. pm_power_off = NULL;
  249. retu_pm_power_off = NULL;
  250. }
  251. mfd_remove_devices(rdev->dev);
  252. regmap_del_irq_chip(i2c->irq, rdev->irq_data);
  253. return 0;
  254. }
  255. static const struct i2c_device_id retu_id[] = {
  256. { "retu-mfd", 0 },
  257. { "tahvo-mfd", 0 },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(i2c, retu_id);
  261. static struct i2c_driver retu_driver = {
  262. .driver = {
  263. .name = "retu-mfd",
  264. },
  265. .probe = retu_probe,
  266. .remove = retu_remove,
  267. .id_table = retu_id,
  268. };
  269. module_i2c_driver(retu_driver);
  270. MODULE_DESCRIPTION("Retu MFD driver");
  271. MODULE_AUTHOR("Juha Yrjölä");
  272. MODULE_AUTHOR("David Weinehall");
  273. MODULE_AUTHOR("Mikko Ylinen");
  274. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
  275. MODULE_LICENSE("GPL");