jz4740-hwmon.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * JZ4740 SoC HWMON driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/err.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/completion.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/hwmon.h>
  25. struct jz4740_hwmon {
  26. struct resource *mem;
  27. void __iomem *base;
  28. int irq;
  29. const struct mfd_cell *cell;
  30. struct device *hwmon;
  31. struct completion read_completion;
  32. struct mutex lock;
  33. };
  34. static ssize_t jz4740_hwmon_show_name(struct device *dev,
  35. struct device_attribute *dev_attr, char *buf)
  36. {
  37. return sprintf(buf, "jz4740\n");
  38. }
  39. static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
  40. {
  41. struct jz4740_hwmon *hwmon = data;
  42. complete(&hwmon->read_completion);
  43. return IRQ_HANDLED;
  44. }
  45. static ssize_t jz4740_hwmon_read_adcin(struct device *dev,
  46. struct device_attribute *dev_attr, char *buf)
  47. {
  48. struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
  49. struct completion *completion = &hwmon->read_completion;
  50. long t;
  51. unsigned long val;
  52. int ret;
  53. mutex_lock(&hwmon->lock);
  54. INIT_COMPLETION(*completion);
  55. enable_irq(hwmon->irq);
  56. hwmon->cell->enable(to_platform_device(dev));
  57. t = wait_for_completion_interruptible_timeout(completion, HZ);
  58. if (t > 0) {
  59. val = readw(hwmon->base) & 0xfff;
  60. val = (val * 3300) >> 12;
  61. ret = sprintf(buf, "%lu\n", val);
  62. } else {
  63. ret = t ? t : -ETIMEDOUT;
  64. }
  65. hwmon->cell->disable(to_platform_device(dev));
  66. disable_irq(hwmon->irq);
  67. mutex_unlock(&hwmon->lock);
  68. return ret;
  69. }
  70. static DEVICE_ATTR(name, S_IRUGO, jz4740_hwmon_show_name, NULL);
  71. static DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL);
  72. static struct attribute *jz4740_hwmon_attributes[] = {
  73. &dev_attr_name.attr,
  74. &dev_attr_in0_input.attr,
  75. NULL
  76. };
  77. static const struct attribute_group jz4740_hwmon_attr_group = {
  78. .attrs = jz4740_hwmon_attributes,
  79. };
  80. static int __devinit jz4740_hwmon_probe(struct platform_device *pdev)
  81. {
  82. int ret;
  83. struct jz4740_hwmon *hwmon;
  84. hwmon = kmalloc(sizeof(*hwmon), GFP_KERNEL);
  85. if (!hwmon) {
  86. dev_err(&pdev->dev, "Failed to allocate driver structure\n");
  87. return -ENOMEM;
  88. }
  89. hwmon->cell = mfd_get_cell(pdev);
  90. hwmon->irq = platform_get_irq(pdev, 0);
  91. if (hwmon->irq < 0) {
  92. ret = hwmon->irq;
  93. dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
  94. goto err_free;
  95. }
  96. hwmon->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  97. if (!hwmon->mem) {
  98. ret = -ENOENT;
  99. dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
  100. goto err_free;
  101. }
  102. hwmon->mem = request_mem_region(hwmon->mem->start,
  103. resource_size(hwmon->mem), pdev->name);
  104. if (!hwmon->mem) {
  105. ret = -EBUSY;
  106. dev_err(&pdev->dev, "Failed to request mmio memory region\n");
  107. goto err_free;
  108. }
  109. hwmon->base = ioremap_nocache(hwmon->mem->start,
  110. resource_size(hwmon->mem));
  111. if (!hwmon->base) {
  112. ret = -EBUSY;
  113. dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
  114. goto err_release_mem_region;
  115. }
  116. init_completion(&hwmon->read_completion);
  117. mutex_init(&hwmon->lock);
  118. platform_set_drvdata(pdev, hwmon);
  119. ret = request_irq(hwmon->irq, jz4740_hwmon_irq, 0, pdev->name, hwmon);
  120. if (ret) {
  121. dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
  122. goto err_iounmap;
  123. }
  124. disable_irq(hwmon->irq);
  125. ret = sysfs_create_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  126. if (ret) {
  127. dev_err(&pdev->dev, "Failed to create sysfs group: %d\n", ret);
  128. goto err_free_irq;
  129. }
  130. hwmon->hwmon = hwmon_device_register(&pdev->dev);
  131. if (IS_ERR(hwmon->hwmon)) {
  132. ret = PTR_ERR(hwmon->hwmon);
  133. goto err_remove_file;
  134. }
  135. return 0;
  136. err_remove_file:
  137. sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  138. err_free_irq:
  139. free_irq(hwmon->irq, hwmon);
  140. err_iounmap:
  141. platform_set_drvdata(pdev, NULL);
  142. iounmap(hwmon->base);
  143. err_release_mem_region:
  144. release_mem_region(hwmon->mem->start, resource_size(hwmon->mem));
  145. err_free:
  146. kfree(hwmon);
  147. return ret;
  148. }
  149. static int __devexit jz4740_hwmon_remove(struct platform_device *pdev)
  150. {
  151. struct jz4740_hwmon *hwmon = platform_get_drvdata(pdev);
  152. hwmon_device_unregister(hwmon->hwmon);
  153. sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  154. free_irq(hwmon->irq, hwmon);
  155. iounmap(hwmon->base);
  156. release_mem_region(hwmon->mem->start, resource_size(hwmon->mem));
  157. platform_set_drvdata(pdev, NULL);
  158. kfree(hwmon);
  159. return 0;
  160. }
  161. static struct platform_driver jz4740_hwmon_driver = {
  162. .probe = jz4740_hwmon_probe,
  163. .remove = __devexit_p(jz4740_hwmon_remove),
  164. .driver = {
  165. .name = "jz4740-hwmon",
  166. .owner = THIS_MODULE,
  167. },
  168. };
  169. module_platform_driver(jz4740_hwmon_driver);
  170. MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
  171. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  172. MODULE_LICENSE("GPL");
  173. MODULE_ALIAS("platform:jz4740-hwmon");