Commit 012f3b9118c7aaa3549f89299386f92b6580d235

Authored by Eric Miao
Committed by Jean Delvare
1 parent 53999bf34d

hwmon: (max1111) Change sysfs interface to in[0-3]_input in millivolts

This patch fixed the inconsistent max1111 sysfs interface as pointed
out by Jean Delvare:

    It was pointed to me that the max1111 driver doesn't implement the
    standard sysfs interface for hwmon drivers (as described in
    Documentation/hwmon/sysfs-interface). It exports files adc[0-3]_in,
    which
    aren't part of the standard interface. Presumably these should be
    renamed to in[0-3]_input. Renaming them is probably not sufficient
    though, as I see no scaling done in the driver. As the MAX1111 chip has
    a documented full scale of 2.048V, I take it that the LSB of the ADC
    has a weight of 8 mV. Exporting raw register values to user-space is
    not OK.

Reported-by: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Eric Miao <eric.y.miao@gmail.com>
Signed-off-by: Jean Delvare <khali@linux-fr.org>

Showing 1 changed file with 9 additions and 6 deletions Inline Diff

drivers/hwmon/max1111.c
1 /* 1 /*
2 * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs 2 * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs
3 * 3 *
4 * Based on arch/arm/mach-pxa/corgi_ssp.c 4 * Based on arch/arm/mach-pxa/corgi_ssp.c
5 * 5 *
6 * Copyright (C) 2004-2005 Richard Purdie 6 * Copyright (C) 2004-2005 Richard Purdie
7 * 7 *
8 * Copyright (C) 2008 Marvell International Ltd. 8 * Copyright (C) 2008 Marvell International Ltd.
9 * Eric Miao <eric.miao@marvell.com> 9 * Eric Miao <eric.miao@marvell.com>
10 * 10 *
11 * This program is free software; you can redistribute it and/or modify 11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as 12 * it under the terms of the GNU General Public License version 2 as
13 * publishhed by the Free Software Foundation. 13 * publishhed by the Free Software Foundation.
14 */ 14 */
15 15
16 #include <linux/module.h> 16 #include <linux/module.h>
17 #include <linux/kernel.h> 17 #include <linux/kernel.h>
18 #include <linux/init.h> 18 #include <linux/init.h>
19 #include <linux/err.h> 19 #include <linux/err.h>
20 #include <linux/hwmon.h> 20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h> 21 #include <linux/hwmon-sysfs.h>
22 #include <linux/spi/spi.h> 22 #include <linux/spi/spi.h>
23 #include <linux/slab.h> 23 #include <linux/slab.h>
24 24
25 #define MAX1111_TX_BUF_SIZE 1 25 #define MAX1111_TX_BUF_SIZE 1
26 #define MAX1111_RX_BUF_SIZE 2 26 #define MAX1111_RX_BUF_SIZE 2
27 27
28 /* MAX1111 Commands */ 28 /* MAX1111 Commands */
29 #define MAX1111_CTRL_PD0 (1u << 0) 29 #define MAX1111_CTRL_PD0 (1u << 0)
30 #define MAX1111_CTRL_PD1 (1u << 1) 30 #define MAX1111_CTRL_PD1 (1u << 1)
31 #define MAX1111_CTRL_SGL (1u << 2) 31 #define MAX1111_CTRL_SGL (1u << 2)
32 #define MAX1111_CTRL_UNI (1u << 3) 32 #define MAX1111_CTRL_UNI (1u << 3)
33 #define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */ 33 #define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
34 #define MAX1111_CTRL_STR (1u << 7) 34 #define MAX1111_CTRL_STR (1u << 7)
35 35
36 struct max1111_data { 36 struct max1111_data {
37 struct spi_device *spi; 37 struct spi_device *spi;
38 struct device *hwmon_dev; 38 struct device *hwmon_dev;
39 struct spi_message msg; 39 struct spi_message msg;
40 struct spi_transfer xfer[2]; 40 struct spi_transfer xfer[2];
41 uint8_t tx_buf[MAX1111_TX_BUF_SIZE]; 41 uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
42 uint8_t rx_buf[MAX1111_RX_BUF_SIZE]; 42 uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
43 struct mutex drvdata_lock; 43 struct mutex drvdata_lock;
44 /* protect msg, xfer and buffers from multiple access */ 44 /* protect msg, xfer and buffers from multiple access */
45 }; 45 };
46 46
47 static int max1111_read(struct device *dev, int channel) 47 static int max1111_read(struct device *dev, int channel)
48 { 48 {
49 struct max1111_data *data = dev_get_drvdata(dev); 49 struct max1111_data *data = dev_get_drvdata(dev);
50 uint8_t v1, v2; 50 uint8_t v1, v2;
51 int err; 51 int err;
52 52
53 /* writing to drvdata struct is not thread safe, wait on mutex */ 53 /* writing to drvdata struct is not thread safe, wait on mutex */
54 mutex_lock(&data->drvdata_lock); 54 mutex_lock(&data->drvdata_lock);
55 55
56 data->tx_buf[0] = (channel << MAX1111_CTRL_SEL_SH) | 56 data->tx_buf[0] = (channel << MAX1111_CTRL_SEL_SH) |
57 MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 | 57 MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
58 MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR; 58 MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;
59 59
60 err = spi_sync(data->spi, &data->msg); 60 err = spi_sync(data->spi, &data->msg);
61 if (err < 0) { 61 if (err < 0) {
62 dev_err(dev, "spi_sync failed with %d\n", err); 62 dev_err(dev, "spi_sync failed with %d\n", err);
63 mutex_unlock(&data->drvdata_lock); 63 mutex_unlock(&data->drvdata_lock);
64 return err; 64 return err;
65 } 65 }
66 66
67 v1 = data->rx_buf[0]; 67 v1 = data->rx_buf[0];
68 v2 = data->rx_buf[1]; 68 v2 = data->rx_buf[1];
69 69
70 mutex_unlock(&data->drvdata_lock); 70 mutex_unlock(&data->drvdata_lock);
71 71
72 if ((v1 & 0xc0) || (v2 & 0x3f)) 72 if ((v1 & 0xc0) || (v2 & 0x3f))
73 return -EINVAL; 73 return -EINVAL;
74 74
75 return (v1 << 2) | (v2 >> 6); 75 return (v1 << 2) | (v2 >> 6);
76 } 76 }
77 77
78 #ifdef CONFIG_SHARPSL_PM 78 #ifdef CONFIG_SHARPSL_PM
79 static struct max1111_data *the_max1111; 79 static struct max1111_data *the_max1111;
80 80
81 int max1111_read_channel(int channel) 81 int max1111_read_channel(int channel)
82 { 82 {
83 return max1111_read(&the_max1111->spi->dev, channel); 83 return max1111_read(&the_max1111->spi->dev, channel);
84 } 84 }
85 EXPORT_SYMBOL(max1111_read_channel); 85 EXPORT_SYMBOL(max1111_read_channel);
86 #endif 86 #endif
87 87
88 /* 88 /*
89 * NOTE: SPI devices do not have a default 'name' attribute, which is 89 * NOTE: SPI devices do not have a default 'name' attribute, which is
90 * likely to be used by hwmon applications to distinguish between 90 * likely to be used by hwmon applications to distinguish between
91 * different devices, explicitly add a name attribute here. 91 * different devices, explicitly add a name attribute here.
92 */ 92 */
93 static ssize_t show_name(struct device *dev, 93 static ssize_t show_name(struct device *dev,
94 struct device_attribute *attr, char *buf) 94 struct device_attribute *attr, char *buf)
95 { 95 {
96 return sprintf(buf, "max1111\n"); 96 return sprintf(buf, "max1111\n");
97 } 97 }
98 98
99 static ssize_t show_adc(struct device *dev, 99 static ssize_t show_adc(struct device *dev,
100 struct device_attribute *attr, char *buf) 100 struct device_attribute *attr, char *buf)
101 { 101 {
102 int channel = to_sensor_dev_attr(attr)->index; 102 int channel = to_sensor_dev_attr(attr)->index;
103 int ret; 103 int ret;
104 104
105 ret = max1111_read(dev, channel); 105 ret = max1111_read(dev, channel);
106 if (ret < 0) 106 if (ret < 0)
107 return ret; 107 return ret;
108 108
109 return sprintf(buf, "%d\n", ret); 109 /* assume the reference voltage to be 2.048V, with an 8-bit sample,
110 * the LSB weight is 8mV
111 */
112 return sprintf(buf, "%d\n", ret * 8);
110 } 113 }
111 114
112 #define MAX1111_ADC_ATTR(_id) \ 115 #define MAX1111_ADC_ATTR(_id) \
113 SENSOR_DEVICE_ATTR(adc##_id##_in, S_IRUGO, show_adc, NULL, _id) 116 SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
114 117
115 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 118 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
116 static MAX1111_ADC_ATTR(0); 119 static MAX1111_ADC_ATTR(0);
117 static MAX1111_ADC_ATTR(1); 120 static MAX1111_ADC_ATTR(1);
118 static MAX1111_ADC_ATTR(2); 121 static MAX1111_ADC_ATTR(2);
119 static MAX1111_ADC_ATTR(3); 122 static MAX1111_ADC_ATTR(3);
120 123
121 static struct attribute *max1111_attributes[] = { 124 static struct attribute *max1111_attributes[] = {
122 &dev_attr_name.attr, 125 &dev_attr_name.attr,
123 &sensor_dev_attr_adc0_in.dev_attr.attr, 126 &sensor_dev_attr_in0_input.dev_attr.attr,
124 &sensor_dev_attr_adc1_in.dev_attr.attr, 127 &sensor_dev_attr_in1_input.dev_attr.attr,
125 &sensor_dev_attr_adc2_in.dev_attr.attr, 128 &sensor_dev_attr_in2_input.dev_attr.attr,
126 &sensor_dev_attr_adc3_in.dev_attr.attr, 129 &sensor_dev_attr_in3_input.dev_attr.attr,
127 NULL, 130 NULL,
128 }; 131 };
129 132
130 static const struct attribute_group max1111_attr_group = { 133 static const struct attribute_group max1111_attr_group = {
131 .attrs = max1111_attributes, 134 .attrs = max1111_attributes,
132 }; 135 };
133 136
134 static int __devinit setup_transfer(struct max1111_data *data) 137 static int __devinit setup_transfer(struct max1111_data *data)
135 { 138 {
136 struct spi_message *m; 139 struct spi_message *m;
137 struct spi_transfer *x; 140 struct spi_transfer *x;
138 141
139 m = &data->msg; 142 m = &data->msg;
140 x = &data->xfer[0]; 143 x = &data->xfer[0];
141 144
142 spi_message_init(m); 145 spi_message_init(m);
143 146
144 x->tx_buf = &data->tx_buf[0]; 147 x->tx_buf = &data->tx_buf[0];
145 x->len = MAX1111_TX_BUF_SIZE; 148 x->len = MAX1111_TX_BUF_SIZE;
146 spi_message_add_tail(x, m); 149 spi_message_add_tail(x, m);
147 150
148 x++; 151 x++;
149 x->rx_buf = &data->rx_buf[0]; 152 x->rx_buf = &data->rx_buf[0];
150 x->len = MAX1111_RX_BUF_SIZE; 153 x->len = MAX1111_RX_BUF_SIZE;
151 spi_message_add_tail(x, m); 154 spi_message_add_tail(x, m);
152 155
153 return 0; 156 return 0;
154 } 157 }
155 158
156 static int __devinit max1111_probe(struct spi_device *spi) 159 static int __devinit max1111_probe(struct spi_device *spi)
157 { 160 {
158 struct max1111_data *data; 161 struct max1111_data *data;
159 int err; 162 int err;
160 163
161 spi->bits_per_word = 8; 164 spi->bits_per_word = 8;
162 spi->mode = SPI_MODE_0; 165 spi->mode = SPI_MODE_0;
163 err = spi_setup(spi); 166 err = spi_setup(spi);
164 if (err < 0) 167 if (err < 0)
165 return err; 168 return err;
166 169
167 data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL); 170 data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL);
168 if (data == NULL) { 171 if (data == NULL) {
169 dev_err(&spi->dev, "failed to allocate memory\n"); 172 dev_err(&spi->dev, "failed to allocate memory\n");
170 return -ENOMEM; 173 return -ENOMEM;
171 } 174 }
172 175
173 err = setup_transfer(data); 176 err = setup_transfer(data);
174 if (err) 177 if (err)
175 goto err_free_data; 178 goto err_free_data;
176 179
177 mutex_init(&data->drvdata_lock); 180 mutex_init(&data->drvdata_lock);
178 181
179 data->spi = spi; 182 data->spi = spi;
180 spi_set_drvdata(spi, data); 183 spi_set_drvdata(spi, data);
181 184
182 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group); 185 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
183 if (err) { 186 if (err) {
184 dev_err(&spi->dev, "failed to create attribute group\n"); 187 dev_err(&spi->dev, "failed to create attribute group\n");
185 goto err_free_data; 188 goto err_free_data;
186 } 189 }
187 190
188 data->hwmon_dev = hwmon_device_register(&spi->dev); 191 data->hwmon_dev = hwmon_device_register(&spi->dev);
189 if (IS_ERR(data->hwmon_dev)) { 192 if (IS_ERR(data->hwmon_dev)) {
190 dev_err(&spi->dev, "failed to create hwmon device\n"); 193 dev_err(&spi->dev, "failed to create hwmon device\n");
191 err = PTR_ERR(data->hwmon_dev); 194 err = PTR_ERR(data->hwmon_dev);
192 goto err_remove; 195 goto err_remove;
193 } 196 }
194 197
195 #ifdef CONFIG_SHARPSL_PM 198 #ifdef CONFIG_SHARPSL_PM
196 the_max1111 = data; 199 the_max1111 = data;
197 #endif 200 #endif
198 return 0; 201 return 0;
199 202
200 err_remove: 203 err_remove:
201 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group); 204 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
202 err_free_data: 205 err_free_data:
203 kfree(data); 206 kfree(data);
204 return err; 207 return err;
205 } 208 }
206 209
207 static int __devexit max1111_remove(struct spi_device *spi) 210 static int __devexit max1111_remove(struct spi_device *spi)
208 { 211 {
209 struct max1111_data *data = spi_get_drvdata(spi); 212 struct max1111_data *data = spi_get_drvdata(spi);
210 213
211 hwmon_device_unregister(data->hwmon_dev); 214 hwmon_device_unregister(data->hwmon_dev);
212 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group); 215 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
213 mutex_destroy(&data->drvdata_lock); 216 mutex_destroy(&data->drvdata_lock);
214 kfree(data); 217 kfree(data);
215 return 0; 218 return 0;
216 } 219 }
217 220
218 static struct spi_driver max1111_driver = { 221 static struct spi_driver max1111_driver = {
219 .driver = { 222 .driver = {
220 .name = "max1111", 223 .name = "max1111",
221 .owner = THIS_MODULE, 224 .owner = THIS_MODULE,
222 }, 225 },
223 .probe = max1111_probe, 226 .probe = max1111_probe,
224 .remove = __devexit_p(max1111_remove), 227 .remove = __devexit_p(max1111_remove),
225 }; 228 };
226 229
227 static int __init max1111_init(void) 230 static int __init max1111_init(void)
228 { 231 {
229 return spi_register_driver(&max1111_driver); 232 return spi_register_driver(&max1111_driver);
230 } 233 }
231 module_init(max1111_init); 234 module_init(max1111_init);
232 235
233 static void __exit max1111_exit(void) 236 static void __exit max1111_exit(void)
234 { 237 {
235 spi_unregister_driver(&max1111_driver); 238 spi_unregister_driver(&max1111_driver);
236 } 239 }
237 module_exit(max1111_exit); 240 module_exit(max1111_exit);
238 241
239 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"); 242 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
240 MODULE_DESCRIPTION("MAX1111 ADC Driver"); 243 MODULE_DESCRIPTION("MAX1111 ADC Driver");
241 MODULE_LICENSE("GPL"); 244 MODULE_LICENSE("GPL");
242 MODULE_ALIAS("spi:max1111"); 245 MODULE_ALIAS("spi:max1111");
243 246