jz4740-hwmon.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. 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. reinit_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 jz4740_hwmon_probe(struct platform_device *pdev)
  81. {
  82. int ret;
  83. struct jz4740_hwmon *hwmon;
  84. struct resource *mem;
  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. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. hwmon->base = devm_ioremap_resource(&pdev->dev, mem);
  97. if (IS_ERR(hwmon->base))
  98. return PTR_ERR(hwmon->base);
  99. init_completion(&hwmon->read_completion);
  100. mutex_init(&hwmon->lock);
  101. platform_set_drvdata(pdev, hwmon);
  102. ret = devm_request_irq(&pdev->dev, hwmon->irq, jz4740_hwmon_irq, 0,
  103. pdev->name, hwmon);
  104. if (ret) {
  105. dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
  106. return ret;
  107. }
  108. disable_irq(hwmon->irq);
  109. ret = sysfs_create_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  110. if (ret) {
  111. dev_err(&pdev->dev, "Failed to create sysfs group: %d\n", ret);
  112. return ret;
  113. }
  114. hwmon->hwmon = hwmon_device_register(&pdev->dev);
  115. if (IS_ERR(hwmon->hwmon)) {
  116. ret = PTR_ERR(hwmon->hwmon);
  117. goto err_remove_file;
  118. }
  119. return 0;
  120. err_remove_file:
  121. sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  122. return ret;
  123. }
  124. static int jz4740_hwmon_remove(struct platform_device *pdev)
  125. {
  126. struct jz4740_hwmon *hwmon = platform_get_drvdata(pdev);
  127. hwmon_device_unregister(hwmon->hwmon);
  128. sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
  129. return 0;
  130. }
  131. static struct platform_driver jz4740_hwmon_driver = {
  132. .probe = jz4740_hwmon_probe,
  133. .remove = jz4740_hwmon_remove,
  134. .driver = {
  135. .name = "jz4740-hwmon",
  136. },
  137. };
  138. module_platform_driver(jz4740_hwmon_driver);
  139. MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
  140. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  141. MODULE_LICENSE("GPL");
  142. MODULE_ALIAS("platform:jz4740-hwmon");