Blame view

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