htc-pasic3.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Core driver for HTC PASIC3 LED/DS1WM chip.
  3. *
  4. * Copyright (C) 2006 Philipp Zabel <philipp.zabel@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/ds1wm.h>
  14. #include <linux/gpio.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/mfd/htc-pasic3.h>
  19. struct pasic3_data {
  20. void __iomem *mapping;
  21. unsigned int bus_shift;
  22. struct platform_device *ds1wm_pdev;
  23. struct platform_device *led_pdev;
  24. };
  25. #define REG_ADDR 5
  26. #define REG_DATA 6
  27. #define READ_MODE 0x80
  28. /*
  29. * write to a secondary register on the PASIC3
  30. */
  31. void pasic3_write_register(struct device *dev, u32 reg, u8 val)
  32. {
  33. struct pasic3_data *asic = dev->driver_data;
  34. int bus_shift = asic->bus_shift;
  35. void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
  36. void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
  37. __raw_writeb(~READ_MODE & reg, addr);
  38. __raw_writeb(val, data);
  39. }
  40. EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */
  41. /*
  42. * read from a secondary register on the PASIC3
  43. */
  44. u8 pasic3_read_register(struct device *dev, u32 reg)
  45. {
  46. struct pasic3_data *asic = dev->driver_data;
  47. int bus_shift = asic->bus_shift;
  48. void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
  49. void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
  50. __raw_writeb(READ_MODE | reg, addr);
  51. return __raw_readb(data);
  52. }
  53. EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */
  54. /*
  55. * LEDs
  56. */
  57. static int led_device_add(struct device *pasic3_dev,
  58. const struct pasic3_leds_machinfo *pdata)
  59. {
  60. struct pasic3_data *asic = pasic3_dev->driver_data;
  61. struct platform_device *pdev;
  62. int ret;
  63. pdev = platform_device_alloc("pasic3-led", -1);
  64. if (!pdev) {
  65. dev_dbg(pasic3_dev, "failed to allocate LED platform device\n");
  66. return -ENOMEM;
  67. }
  68. ret = platform_device_add_data(pdev, pdata,
  69. sizeof(struct pasic3_leds_machinfo));
  70. if (ret < 0) {
  71. dev_dbg(pasic3_dev, "failed to add LED platform data\n");
  72. goto exit_pdev_put;
  73. }
  74. pdev->dev.parent = pasic3_dev;
  75. ret = platform_device_add(pdev);
  76. if (ret < 0) {
  77. dev_dbg(pasic3_dev, "failed to add LED platform device\n");
  78. goto exit_pdev_put;
  79. }
  80. asic->led_pdev = pdev;
  81. return 0;
  82. exit_pdev_put:
  83. platform_device_put(pdev);
  84. return ret;
  85. }
  86. /*
  87. * DS1WM
  88. */
  89. static void ds1wm_enable(struct platform_device *pdev)
  90. {
  91. struct device *dev = pdev->dev.parent;
  92. int c;
  93. c = pasic3_read_register(dev, 0x28);
  94. pasic3_write_register(dev, 0x28, c & 0x7f);
  95. dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f);
  96. }
  97. static void ds1wm_disable(struct platform_device *pdev)
  98. {
  99. struct device *dev = pdev->dev.parent;
  100. int c;
  101. c = pasic3_read_register(dev, 0x28);
  102. pasic3_write_register(dev, 0x28, c | 0x80);
  103. dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80);
  104. }
  105. static struct ds1wm_platform_data ds1wm_pdata = {
  106. .bus_shift = 2,
  107. .enable = ds1wm_enable,
  108. .disable = ds1wm_disable,
  109. };
  110. static int ds1wm_device_add(struct device *pasic3_dev, int bus_shift)
  111. {
  112. struct pasic3_data *asic = pasic3_dev->driver_data;
  113. struct platform_device *pdev;
  114. int ret;
  115. pdev = platform_device_alloc("ds1wm", -1);
  116. if (!pdev) {
  117. dev_dbg(pasic3_dev, "failed to allocate DS1WM platform device\n");
  118. return -ENOMEM;
  119. }
  120. ret = platform_device_add_resources(pdev, pdev->resource,
  121. pdev->num_resources);
  122. if (ret < 0) {
  123. dev_dbg(pasic3_dev, "failed to add DS1WM resources\n");
  124. goto exit_pdev_put;
  125. }
  126. ds1wm_pdata.bus_shift = asic->bus_shift;
  127. ret = platform_device_add_data(pdev, &ds1wm_pdata,
  128. sizeof(struct ds1wm_platform_data));
  129. if (ret < 0) {
  130. dev_dbg(pasic3_dev, "failed to add DS1WM platform data\n");
  131. goto exit_pdev_put;
  132. }
  133. pdev->dev.parent = pasic3_dev;
  134. ret = platform_device_add(pdev);
  135. if (ret < 0) {
  136. dev_dbg(pasic3_dev, "failed to add DS1WM platform device\n");
  137. goto exit_pdev_put;
  138. }
  139. asic->ds1wm_pdev = pdev;
  140. return 0;
  141. exit_pdev_put:
  142. platform_device_put(pdev);
  143. return ret;
  144. }
  145. static int __init pasic3_probe(struct platform_device *pdev)
  146. {
  147. struct pasic3_platform_data *pdata = pdev->dev.platform_data;
  148. struct device *dev = &pdev->dev;
  149. struct pasic3_data *asic;
  150. struct resource *r;
  151. int ret;
  152. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  153. if (!r)
  154. return -ENXIO;
  155. if (!request_mem_region(r->start, r->end - r->start + 1, "pasic3"))
  156. return -EBUSY;
  157. asic = kzalloc(sizeof(struct pasic3_data), GFP_KERNEL);
  158. if (!asic)
  159. return -ENOMEM;
  160. platform_set_drvdata(pdev, asic);
  161. if (pdata && pdata->bus_shift)
  162. asic->bus_shift = pdata->bus_shift;
  163. else
  164. asic->bus_shift = 2;
  165. asic->mapping = ioremap(r->start, r->end - r->start + 1);
  166. if (!asic->mapping) {
  167. dev_err(dev, "couldn't ioremap PASIC3\n");
  168. kfree(asic);
  169. return -ENOMEM;
  170. }
  171. ret = ds1wm_device_add(dev, asic->bus_shift);
  172. if (ret < 0)
  173. dev_warn(dev, "failed to register DS1WM\n");
  174. if (pdata->led_pdata) {
  175. ret = led_device_add(dev, pdata->led_pdata);
  176. if (ret < 0)
  177. dev_warn(dev, "failed to register LED device\n");
  178. }
  179. return 0;
  180. }
  181. static int pasic3_remove(struct platform_device *pdev)
  182. {
  183. struct pasic3_data *asic = platform_get_drvdata(pdev);
  184. struct resource *r;
  185. if (asic->led_pdev)
  186. platform_device_unregister(asic->led_pdev);
  187. if (asic->ds1wm_pdev)
  188. platform_device_unregister(asic->ds1wm_pdev);
  189. iounmap(asic->mapping);
  190. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. release_mem_region(r->start, r->end - r->start + 1);
  192. kfree(asic);
  193. return 0;
  194. }
  195. static struct platform_driver pasic3_driver = {
  196. .driver = {
  197. .name = "pasic3",
  198. },
  199. .remove = pasic3_remove,
  200. };
  201. static int __init pasic3_base_init(void)
  202. {
  203. return platform_driver_probe(&pasic3_driver, pasic3_probe);
  204. }
  205. static void __exit pasic3_base_exit(void)
  206. {
  207. platform_driver_unregister(&pasic3_driver);
  208. }
  209. module_init(pasic3_base_init);
  210. module_exit(pasic3_base_exit);
  211. MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
  212. MODULE_DESCRIPTION("Core driver for HTC PASIC3");
  213. MODULE_LICENSE("GPL");