jz4740-hwmon.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 platform_device *pdev;
  31. struct completion read_completion;
  32. struct mutex lock;
  33. };
  34. static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
  35. {
  36. struct jz4740_hwmon *hwmon = data;
  37. complete(&hwmon->read_completion);
  38. return IRQ_HANDLED;
  39. }
  40. static ssize_t jz4740_hwmon_read_adcin(struct device *dev,
  41. struct device_attribute *dev_attr, char *buf)
  42. {
  43. struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
  44. struct platform_device *pdev = hwmon->pdev;
  45. struct completion *completion = &hwmon->read_completion;
  46. long t;
  47. unsigned long val;
  48. int ret;
  49. mutex_lock(&hwmon->lock);
  50. reinit_completion(completion);
  51. enable_irq(hwmon->irq);
  52. hwmon->cell->enable(pdev);
  53. t = wait_for_completion_interruptible_timeout(completion, HZ);
  54. if (t > 0) {
  55. val = readw(hwmon->base) & 0xfff;
  56. val = (val * 3300) >> 12;
  57. ret = sprintf(buf, "%lu\n", val);
  58. } else {
  59. ret = t ? t : -ETIMEDOUT;
  60. }
  61. hwmon->cell->disable(pdev);
  62. disable_irq(hwmon->irq);
  63. mutex_unlock(&hwmon->lock);
  64. return ret;
  65. }
  66. static DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL);
  67. static struct attribute *jz4740_attrs[] = {
  68. &dev_attr_in0_input.attr,
  69. NULL
  70. };
  71. ATTRIBUTE_GROUPS(jz4740);
  72. static int jz4740_hwmon_probe(struct platform_device *pdev)
  73. {
  74. int ret;
  75. struct device *dev = &pdev->dev;
  76. struct jz4740_hwmon *hwmon;
  77. struct device *hwmon_dev;
  78. struct resource *mem;
  79. hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
  80. if (!hwmon)
  81. return -ENOMEM;
  82. hwmon->cell = mfd_get_cell(pdev);
  83. hwmon->irq = platform_get_irq(pdev, 0);
  84. if (hwmon->irq < 0) {
  85. dev_err(&pdev->dev, "Failed to get platform irq: %d\n",
  86. hwmon->irq);
  87. return hwmon->irq;
  88. }
  89. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  90. hwmon->base = devm_ioremap_resource(&pdev->dev, mem);
  91. if (IS_ERR(hwmon->base))
  92. return PTR_ERR(hwmon->base);
  93. hwmon->pdev = pdev;
  94. init_completion(&hwmon->read_completion);
  95. mutex_init(&hwmon->lock);
  96. ret = devm_request_irq(dev, hwmon->irq, jz4740_hwmon_irq, 0,
  97. pdev->name, hwmon);
  98. if (ret) {
  99. dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
  100. return ret;
  101. }
  102. disable_irq(hwmon->irq);
  103. hwmon_dev = devm_hwmon_device_register_with_groups(dev, "jz4740", hwmon,
  104. jz4740_groups);
  105. return PTR_ERR_OR_ZERO(hwmon_dev);
  106. }
  107. static struct platform_driver jz4740_hwmon_driver = {
  108. .probe = jz4740_hwmon_probe,
  109. .driver = {
  110. .name = "jz4740-hwmon",
  111. },
  112. };
  113. module_platform_driver(jz4740_hwmon_driver);
  114. MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
  115. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  116. MODULE_LICENSE("GPL");
  117. MODULE_ALIAS("platform:jz4740-hwmon");