Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 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 | |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/slab.h> |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 23 | #include <linux/io.h> |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 24 | |
| 25 | #include <linux/completion.h> |
| 26 | #include <linux/mfd/core.h> |
| 27 | |
| 28 | #include <linux/hwmon.h> |
| 29 | |
| 30 | struct jz4740_hwmon { |
| 31 | struct resource *mem; |
| 32 | void __iomem *base; |
| 33 | |
| 34 | int irq; |
| 35 | |
Andres Salomon | e930006 | 2011-03-01 13:35:48 -0800 | [diff] [blame] | 36 | const struct mfd_cell *cell; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 37 | struct device *hwmon; |
| 38 | |
| 39 | struct completion read_completion; |
| 40 | |
| 41 | struct mutex lock; |
| 42 | }; |
| 43 | |
| 44 | static ssize_t jz4740_hwmon_show_name(struct device *dev, |
| 45 | struct device_attribute *dev_attr, char *buf) |
| 46 | { |
| 47 | return sprintf(buf, "jz4740\n"); |
| 48 | } |
| 49 | |
| 50 | static irqreturn_t jz4740_hwmon_irq(int irq, void *data) |
| 51 | { |
| 52 | struct jz4740_hwmon *hwmon = data; |
| 53 | |
| 54 | complete(&hwmon->read_completion); |
| 55 | return IRQ_HANDLED; |
| 56 | } |
| 57 | |
| 58 | static ssize_t jz4740_hwmon_read_adcin(struct device *dev, |
| 59 | struct device_attribute *dev_attr, char *buf) |
| 60 | { |
| 61 | struct jz4740_hwmon *hwmon = dev_get_drvdata(dev); |
| 62 | struct completion *completion = &hwmon->read_completion; |
Axel Lin | 0b57d76 | 2011-12-08 08:04:12 -0500 | [diff] [blame] | 63 | long t; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 64 | unsigned long val; |
| 65 | int ret; |
| 66 | |
| 67 | mutex_lock(&hwmon->lock); |
| 68 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 69 | reinit_completion(completion); |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 70 | |
| 71 | enable_irq(hwmon->irq); |
| 72 | hwmon->cell->enable(to_platform_device(dev)); |
| 73 | |
| 74 | t = wait_for_completion_interruptible_timeout(completion, HZ); |
| 75 | |
| 76 | if (t > 0) { |
| 77 | val = readw(hwmon->base) & 0xfff; |
| 78 | val = (val * 3300) >> 12; |
| 79 | ret = sprintf(buf, "%lu\n", val); |
| 80 | } else { |
| 81 | ret = t ? t : -ETIMEDOUT; |
| 82 | } |
| 83 | |
| 84 | hwmon->cell->disable(to_platform_device(dev)); |
| 85 | disable_irq(hwmon->irq); |
| 86 | |
| 87 | mutex_unlock(&hwmon->lock); |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | static DEVICE_ATTR(name, S_IRUGO, jz4740_hwmon_show_name, NULL); |
| 93 | static DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL); |
| 94 | |
| 95 | static struct attribute *jz4740_hwmon_attributes[] = { |
| 96 | &dev_attr_name.attr, |
| 97 | &dev_attr_in0_input.attr, |
| 98 | NULL |
| 99 | }; |
| 100 | |
| 101 | static const struct attribute_group jz4740_hwmon_attr_group = { |
| 102 | .attrs = jz4740_hwmon_attributes, |
| 103 | }; |
| 104 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 105 | static int jz4740_hwmon_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 106 | { |
| 107 | int ret; |
| 108 | struct jz4740_hwmon *hwmon; |
| 109 | |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 110 | hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL); |
| 111 | if (!hwmon) |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 112 | return -ENOMEM; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 113 | |
Andres Salomon | 6a54ac2 | 2011-02-17 19:07:10 -0800 | [diff] [blame] | 114 | hwmon->cell = mfd_get_cell(pdev); |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 115 | |
| 116 | hwmon->irq = platform_get_irq(pdev, 0); |
| 117 | if (hwmon->irq < 0) { |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 118 | dev_err(&pdev->dev, "Failed to get platform irq: %d\n", |
| 119 | hwmon->irq); |
| 120 | return hwmon->irq; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | hwmon->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 124 | if (!hwmon->mem) { |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 125 | dev_err(&pdev->dev, "Failed to get platform mmio resource\n"); |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 126 | return -ENOENT; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 129 | hwmon->mem = devm_request_mem_region(&pdev->dev, hwmon->mem->start, |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 130 | resource_size(hwmon->mem), pdev->name); |
| 131 | if (!hwmon->mem) { |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 132 | dev_err(&pdev->dev, "Failed to request mmio memory region\n"); |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 133 | return -EBUSY; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 136 | hwmon->base = devm_ioremap_nocache(&pdev->dev, hwmon->mem->start, |
| 137 | resource_size(hwmon->mem)); |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 138 | if (!hwmon->base) { |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 139 | dev_err(&pdev->dev, "Failed to ioremap mmio memory\n"); |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 140 | return -EBUSY; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | init_completion(&hwmon->read_completion); |
| 144 | mutex_init(&hwmon->lock); |
| 145 | |
| 146 | platform_set_drvdata(pdev, hwmon); |
| 147 | |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 148 | ret = devm_request_irq(&pdev->dev, hwmon->irq, jz4740_hwmon_irq, 0, |
| 149 | pdev->name, hwmon); |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 150 | if (ret) { |
| 151 | dev_err(&pdev->dev, "Failed to request irq: %d\n", ret); |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 152 | return ret; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 153 | } |
| 154 | disable_irq(hwmon->irq); |
| 155 | |
| 156 | ret = sysfs_create_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group); |
| 157 | if (ret) { |
| 158 | dev_err(&pdev->dev, "Failed to create sysfs group: %d\n", ret); |
Guenter Roeck | b25df2b | 2012-06-02 12:22:08 -0700 | [diff] [blame] | 159 | return ret; |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | hwmon->hwmon = hwmon_device_register(&pdev->dev); |
| 163 | if (IS_ERR(hwmon->hwmon)) { |
| 164 | ret = PTR_ERR(hwmon->hwmon); |
| 165 | goto err_remove_file; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | |
| 170 | err_remove_file: |
| 171 | sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group); |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 172 | return ret; |
| 173 | } |
| 174 | |
Bill Pemberton | 281dfd0 | 2012-11-19 13:25:51 -0500 | [diff] [blame] | 175 | static int jz4740_hwmon_remove(struct platform_device *pdev) |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 176 | { |
| 177 | struct jz4740_hwmon *hwmon = platform_get_drvdata(pdev); |
| 178 | |
| 179 | hwmon_device_unregister(hwmon->hwmon); |
| 180 | sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group); |
| 181 | |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Axel Lin | d6c4f2a | 2011-12-08 08:33:37 -0500 | [diff] [blame] | 185 | static struct platform_driver jz4740_hwmon_driver = { |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 186 | .probe = jz4740_hwmon_probe, |
Bill Pemberton | 9e5e9b7 | 2012-11-19 13:21:20 -0500 | [diff] [blame] | 187 | .remove = jz4740_hwmon_remove, |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 188 | .driver = { |
| 189 | .name = "jz4740-hwmon", |
| 190 | .owner = THIS_MODULE, |
| 191 | }, |
| 192 | }; |
| 193 | |
Axel Lin | 25a236a | 2011-11-25 02:31:00 -0500 | [diff] [blame] | 194 | module_platform_driver(jz4740_hwmon_driver); |
Lars-Peter Clausen | 7f983ba | 2010-06-19 18:32:58 +0000 | [diff] [blame] | 195 | |
| 196 | MODULE_DESCRIPTION("JZ4740 SoC HWMON driver"); |
| 197 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 198 | MODULE_LICENSE("GPL"); |
| 199 | MODULE_ALIAS("platform:jz4740-hwmon"); |