retu-mfd.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Retu 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/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/mutex.h>
  24. #include <linux/module.h>
  25. #include <linux/regmap.h>
  26. #include <linux/mfd/core.h>
  27. #include <linux/mfd/retu.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/moduleparam.h>
  30. /* Registers */
  31. #define RETU_REG_ASICR 0x00 /* ASIC ID and revision */
  32. #define RETU_REG_ASICR_VILMA (1 << 7) /* Bit indicating Vilma */
  33. #define RETU_REG_IDR 0x01 /* Interrupt ID */
  34. #define RETU_REG_IMR 0x02 /* Interrupt mask */
  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 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. int retu_read(struct retu_dev *rdev, u8 reg)
  78. {
  79. int ret;
  80. int value;
  81. mutex_lock(&rdev->mutex);
  82. ret = regmap_read(rdev->regmap, reg, &value);
  83. mutex_unlock(&rdev->mutex);
  84. return ret ? ret : value;
  85. }
  86. EXPORT_SYMBOL_GPL(retu_read);
  87. int retu_write(struct retu_dev *rdev, u8 reg, u16 data)
  88. {
  89. int ret;
  90. mutex_lock(&rdev->mutex);
  91. ret = regmap_write(rdev->regmap, reg, data);
  92. mutex_unlock(&rdev->mutex);
  93. return ret;
  94. }
  95. EXPORT_SYMBOL_GPL(retu_write);
  96. static void retu_power_off(void)
  97. {
  98. struct retu_dev *rdev = retu_pm_power_off;
  99. int reg;
  100. mutex_lock(&retu_pm_power_off->mutex);
  101. /* Ignore power button state */
  102. regmap_read(rdev->regmap, RETU_REG_CC1, &reg);
  103. regmap_write(rdev->regmap, RETU_REG_CC1, reg | 2);
  104. /* Expire watchdog immediately */
  105. regmap_write(rdev->regmap, RETU_REG_WATCHDOG, 0);
  106. /* Wait for poweroff */
  107. for (;;)
  108. cpu_relax();
  109. mutex_unlock(&retu_pm_power_off->mutex);
  110. }
  111. static int retu_regmap_read(void *context, const void *reg, size_t reg_size,
  112. void *val, size_t val_size)
  113. {
  114. int ret;
  115. struct device *dev = context;
  116. struct i2c_client *i2c = to_i2c_client(dev);
  117. BUG_ON(reg_size != 1 || val_size != 2);
  118. ret = i2c_smbus_read_word_data(i2c, *(u8 const *)reg);
  119. if (ret < 0)
  120. return ret;
  121. *(u16 *)val = ret;
  122. return 0;
  123. }
  124. static int retu_regmap_write(void *context, const void *data, size_t count)
  125. {
  126. u8 reg;
  127. u16 val;
  128. struct device *dev = context;
  129. struct i2c_client *i2c = to_i2c_client(dev);
  130. BUG_ON(count != sizeof(reg) + sizeof(val));
  131. memcpy(&reg, data, sizeof(reg));
  132. memcpy(&val, data + sizeof(reg), sizeof(val));
  133. return i2c_smbus_write_word_data(i2c, reg, val);
  134. }
  135. static struct regmap_bus retu_bus = {
  136. .read = retu_regmap_read,
  137. .write = retu_regmap_write,
  138. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  139. };
  140. static struct regmap_config retu_config = {
  141. .reg_bits = 8,
  142. .val_bits = 16,
  143. };
  144. static int retu_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  145. {
  146. struct retu_dev *rdev;
  147. int ret;
  148. rdev = devm_kzalloc(&i2c->dev, sizeof(*rdev), GFP_KERNEL);
  149. if (rdev == NULL)
  150. return -ENOMEM;
  151. i2c_set_clientdata(i2c, rdev);
  152. rdev->dev = &i2c->dev;
  153. mutex_init(&rdev->mutex);
  154. rdev->regmap = devm_regmap_init(&i2c->dev, &retu_bus, &i2c->dev,
  155. &retu_config);
  156. if (IS_ERR(rdev->regmap))
  157. return PTR_ERR(rdev->regmap);
  158. ret = retu_read(rdev, RETU_REG_ASICR);
  159. if (ret < 0) {
  160. dev_err(rdev->dev, "could not read Retu revision: %d\n", ret);
  161. return ret;
  162. }
  163. dev_info(rdev->dev, "Retu%s v%d.%d found\n",
  164. (ret & RETU_REG_ASICR_VILMA) ? " & Vilma" : "",
  165. (ret >> 4) & 0x7, ret & 0xf);
  166. /* Mask all RETU interrupts. */
  167. ret = retu_write(rdev, RETU_REG_IMR, 0xffff);
  168. if (ret < 0)
  169. return ret;
  170. ret = regmap_add_irq_chip(rdev->regmap, i2c->irq, IRQF_ONESHOT, -1,
  171. &retu_irq_chip, &rdev->irq_data);
  172. if (ret < 0)
  173. return ret;
  174. ret = mfd_add_devices(rdev->dev, -1, retu_devs, ARRAY_SIZE(retu_devs),
  175. NULL, regmap_irq_chip_get_base(rdev->irq_data),
  176. NULL);
  177. if (ret < 0) {
  178. regmap_del_irq_chip(i2c->irq, rdev->irq_data);
  179. return ret;
  180. }
  181. if (!pm_power_off) {
  182. retu_pm_power_off = rdev;
  183. pm_power_off = retu_power_off;
  184. }
  185. return 0;
  186. }
  187. static int retu_remove(struct i2c_client *i2c)
  188. {
  189. struct retu_dev *rdev = i2c_get_clientdata(i2c);
  190. if (retu_pm_power_off == rdev) {
  191. pm_power_off = NULL;
  192. retu_pm_power_off = NULL;
  193. }
  194. mfd_remove_devices(rdev->dev);
  195. regmap_del_irq_chip(i2c->irq, rdev->irq_data);
  196. return 0;
  197. }
  198. static const struct i2c_device_id retu_id[] = {
  199. { "retu-mfd", 0 },
  200. { }
  201. };
  202. MODULE_DEVICE_TABLE(i2c, retu_id);
  203. static struct i2c_driver retu_driver = {
  204. .driver = {
  205. .name = "retu-mfd",
  206. .owner = THIS_MODULE,
  207. },
  208. .probe = retu_probe,
  209. .remove = retu_remove,
  210. .id_table = retu_id,
  211. };
  212. module_i2c_driver(retu_driver);
  213. MODULE_DESCRIPTION("Retu MFD driver");
  214. MODULE_AUTHOR("Juha Yrjölä");
  215. MODULE_AUTHOR("David Weinehall");
  216. MODULE_AUTHOR("Mikko Ylinen");
  217. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
  218. MODULE_LICENSE("GPL");