jz4740-hwmon.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/io.h>
  23. #include <linux/completion.h>
  24. #include <linux/mfd/core.h>
  25. #include <linux/hwmon.h>
  26. struct jz4740_hwmon {
  27. struct resource *mem;
  28. void __iomem *base;
  29. int irq;
  30. const struct mfd_cell *cell;
  31. struct device *hwmon;
  32. struct completion read_completion;
  33. struct mutex lock;
  34. };
  35. static ssize_t jz4740_hwmon_show_name(struct device *dev,
  36. struct device_attribute *dev_attr, char *buf)
  37. {
  38. return sprintf(buf, "jz4740\n");
  39. }
  40. static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
  41. {
  42. struct jz4740_hwmon *hwmon = data;
  43. complete(&hwmon->read_completion);
  44. return IRQ_HANDLED;
  45. }
  46. static ssize_t jz4740_hwmon_read_adcin(struct device *dev,
  47. struct device_attribute *dev_attr, char *buf)
  48. {
  49. struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
  50. struct completion *completion = &hwmon->read_completion;
  51. long t;
  52. unsigned long val;
  53. int ret;
  54. mutex_lock(&hwmon->lock);
  55. INIT_COMPLETION(*completion);
  56. enable_irq(hwmon->irq);
  57. hwmon->cell->enable(to_platform_device(dev));
  58. t = wait_for_completion_interruptible_timeout(completion, HZ);
  59. if (t > 0) {
  60. val = readw(hwmon->base) & 0xfff;
  61. val = (val * 3300) >> 12;
  62. ret = sprintf(buf, "%lu\n", val);
  63. } else {
  64. ret = t ? t : -ETIMEDOUT;
  65. }
  66. hwmon->cell->disable(to_platform_device(dev));
  67. disable_irq(hwmon->irq);
  68. mutex_unlock(&hwmon->lock);
  69. return ret;
  70. }
  71. static DEVICE_ATTR(name, S_IRUGO, jz4740_hwmon_show_name, NULL);
  72. static DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL);
  73. static struct attribute *jz4740_hwmon_attributes[] = {
  74. &dev_attr_name.attr,
  75. &dev_attr_in0_input.attr,
  76. NULL
  77. };
  78. static const struct attribute_group jz4740_hwmon_attr_group = {
  79. .attrs = jz4740_hwmon_attributes,
  80. };
  81. static int jz4740_hwmon_probe(struct platform_device *pdev)
  82. {
  83. int ret;
  84. struct jz4740_hwmon *hwmon;
  85. hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
  86. if (!hwmon)
  87. return -ENOMEM;
  88. hwmon->cell = mfd_get_cell(pdev);
  89. hwmon->irq = platform_get_irq(pdev, 0);
  90. if (hwmon->irq < 0) {
  91. dev_err(&pdev->dev, "Failed to get platform irq: %d\n",
  92. hwmon->irq);
  93. return hwmon->irq;
  94. }
  95. hwmon->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. if (!hwmon->mem) {
  97. dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
  98. return -ENOENT;
  99. }
  100. hwmon->mem = devm_request_mem_region(&pdev->dev, hwmon->mem->start,
  101. resource_size(hwmon->mem), pdev->name);
  102. if (!hwmon->mem) {
  103. dev_err(&pdev->dev, "Failed to request mmio memory region\n");
  104. return -EBUSY;
  105. }
  106. hwmon->base = devm_ioremap_nocache(&pdev->dev, hwmon->mem->start,
  107. resource_size(hwmon->mem));
  108. if (!hwmon->base) {
  109. dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
  110. return -EBUSY;
  111. }
  112. init_completion(&hwmon->read_completion);
  113. mutex_init(&hwmon->lock);
  114. platform_set_drvdata(pdev, hwmon);
  115. ret = devm_request_irq(&pdev->dev, hwmon->irq, jz4740_hwmon_irq, 0,
  116. pdev->name, hwmon);
  117. if (ret) {
  118. dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
  119. return ret;
  120. }
  121. disable_irq(hwmon->irq);
  122. ret = sysfs_create_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  123. if (ret) {
  124. dev_err(&pdev->dev, "Failed to create sysfs group: %d\n", ret);
  125. return ret;
  126. }
  127. hwmon->hwmon = hwmon_device_register(&pdev->dev);
  128. if (IS_ERR(hwmon->hwmon)) {
  129. ret = PTR_ERR(hwmon->hwmon);
  130. goto err_remove_file;
  131. }
  132. return 0;
  133. err_remove_file:
  134. sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  135. return ret;
  136. }
  137. static int jz4740_hwmon_remove(struct platform_device *pdev)
  138. {
  139. struct jz4740_hwmon *hwmon = platform_get_drvdata(pdev);
  140. hwmon_device_unregister(hwmon->hwmon);
  141. sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  142. return 0;
  143. }
  144. static struct platform_driver jz4740_hwmon_driver = {
  145. .probe = jz4740_hwmon_probe,
  146. .remove = jz4740_hwmon_remove,
  147. .driver = {
  148. .name = "jz4740-hwmon",
  149. .owner = THIS_MODULE,
  150. },
  151. };
  152. module_platform_driver(jz4740_hwmon_driver);
  153. MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
  154. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  155. MODULE_LICENSE("GPL");
  156. MODULE_ALIAS("platform:jz4740-hwmon");