• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * STMicroelectronics magnetometers driver
3  *
4  * Copyright 2012-2013 STMicroelectronics Inc.
5  *
6  * Denis Ciocca <denis.ciocca@st.com>
7  *
8  * Licensed under the GPL-2.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/mutex.h>
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
20 #include <linux/irq.h>
21 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
25 
26 #include <linux/iio/common/st_sensors.h>
27 #include "st_magn.h"
28 
29 #define ST_MAGN_NUMBER_DATA_CHANNELS		3
30 
31 /* DEFAULT VALUE FOR SENSORS */
32 #define ST_MAGN_DEFAULT_OUT_X_H_ADDR		0X03
33 #define ST_MAGN_DEFAULT_OUT_Y_H_ADDR		0X07
34 #define ST_MAGN_DEFAULT_OUT_Z_H_ADDR		0X05
35 
36 /* FULLSCALE */
37 #define ST_MAGN_FS_AVL_1300MG			1300
38 #define ST_MAGN_FS_AVL_1900MG			1900
39 #define ST_MAGN_FS_AVL_2500MG			2500
40 #define ST_MAGN_FS_AVL_4000MG			4000
41 #define ST_MAGN_FS_AVL_4700MG			4700
42 #define ST_MAGN_FS_AVL_5600MG			5600
43 #define ST_MAGN_FS_AVL_8000MG			8000
44 #define ST_MAGN_FS_AVL_8100MG			8100
45 #define ST_MAGN_FS_AVL_12000MG			12000
46 #define ST_MAGN_FS_AVL_16000MG			16000
47 
48 /* CUSTOM VALUES FOR SENSOR 1 */
49 #define ST_MAGN_1_WAI_EXP			0x3c
50 #define ST_MAGN_1_ODR_ADDR			0x00
51 #define ST_MAGN_1_ODR_MASK			0x1c
52 #define ST_MAGN_1_ODR_AVL_1HZ_VAL		0x00
53 #define ST_MAGN_1_ODR_AVL_2HZ_VAL		0x01
54 #define ST_MAGN_1_ODR_AVL_3HZ_VAL		0x02
55 #define ST_MAGN_1_ODR_AVL_8HZ_VAL		0x03
56 #define ST_MAGN_1_ODR_AVL_15HZ_VAL		0x04
57 #define ST_MAGN_1_ODR_AVL_30HZ_VAL		0x05
58 #define ST_MAGN_1_ODR_AVL_75HZ_VAL		0x06
59 #define ST_MAGN_1_ODR_AVL_220HZ_VAL		0x07
60 #define ST_MAGN_1_PW_ADDR			0x02
61 #define ST_MAGN_1_PW_MASK			0x03
62 #define ST_MAGN_1_PW_ON				0x00
63 #define ST_MAGN_1_PW_OFF			0x03
64 #define ST_MAGN_1_FS_ADDR			0x01
65 #define ST_MAGN_1_FS_MASK			0xe0
66 #define ST_MAGN_1_FS_AVL_1300_VAL		0x01
67 #define ST_MAGN_1_FS_AVL_1900_VAL		0x02
68 #define ST_MAGN_1_FS_AVL_2500_VAL		0x03
69 #define ST_MAGN_1_FS_AVL_4000_VAL		0x04
70 #define ST_MAGN_1_FS_AVL_4700_VAL		0x05
71 #define ST_MAGN_1_FS_AVL_5600_VAL		0x06
72 #define ST_MAGN_1_FS_AVL_8100_VAL		0x07
73 #define ST_MAGN_1_FS_AVL_1300_GAIN_XY		909
74 #define ST_MAGN_1_FS_AVL_1900_GAIN_XY		1169
75 #define ST_MAGN_1_FS_AVL_2500_GAIN_XY		1492
76 #define ST_MAGN_1_FS_AVL_4000_GAIN_XY		2222
77 #define ST_MAGN_1_FS_AVL_4700_GAIN_XY		2500
78 #define ST_MAGN_1_FS_AVL_5600_GAIN_XY		3030
79 #define ST_MAGN_1_FS_AVL_8100_GAIN_XY		4347
80 #define ST_MAGN_1_FS_AVL_1300_GAIN_Z		1020
81 #define ST_MAGN_1_FS_AVL_1900_GAIN_Z		1315
82 #define ST_MAGN_1_FS_AVL_2500_GAIN_Z		1666
83 #define ST_MAGN_1_FS_AVL_4000_GAIN_Z		2500
84 #define ST_MAGN_1_FS_AVL_4700_GAIN_Z		2816
85 #define ST_MAGN_1_FS_AVL_5600_GAIN_Z		3389
86 #define ST_MAGN_1_FS_AVL_8100_GAIN_Z		4878
87 #define ST_MAGN_1_MULTIREAD_BIT			false
88 
89 /* CUSTOM VALUES FOR SENSOR 2 */
90 #define ST_MAGN_2_WAI_EXP			0x3d
91 #define ST_MAGN_2_ODR_ADDR			0x20
92 #define ST_MAGN_2_ODR_MASK			0x1c
93 #define ST_MAGN_2_ODR_AVL_1HZ_VAL		0x00
94 #define ST_MAGN_2_ODR_AVL_2HZ_VAL		0x01
95 #define ST_MAGN_2_ODR_AVL_3HZ_VAL		0x02
96 #define ST_MAGN_2_ODR_AVL_5HZ_VAL		0x03
97 #define ST_MAGN_2_ODR_AVL_10HZ_VAL		0x04
98 #define ST_MAGN_2_ODR_AVL_20HZ_VAL		0x05
99 #define ST_MAGN_2_ODR_AVL_40HZ_VAL		0x06
100 #define ST_MAGN_2_ODR_AVL_80HZ_VAL		0x07
101 #define ST_MAGN_2_PW_ADDR			0x22
102 #define ST_MAGN_2_PW_MASK			0x03
103 #define ST_MAGN_2_PW_ON				0x00
104 #define ST_MAGN_2_PW_OFF			0x03
105 #define ST_MAGN_2_FS_ADDR			0x21
106 #define ST_MAGN_2_FS_MASK			0x60
107 #define ST_MAGN_2_FS_AVL_4000_VAL		0x00
108 #define ST_MAGN_2_FS_AVL_8000_VAL		0x01
109 #define ST_MAGN_2_FS_AVL_12000_VAL		0x02
110 #define ST_MAGN_2_FS_AVL_16000_VAL		0x03
111 #define ST_MAGN_2_FS_AVL_4000_GAIN		146
112 #define ST_MAGN_2_FS_AVL_8000_GAIN		292
113 #define ST_MAGN_2_FS_AVL_12000_GAIN		438
114 #define ST_MAGN_2_FS_AVL_16000_GAIN		584
115 #define ST_MAGN_2_MULTIREAD_BIT			false
116 #define ST_MAGN_2_OUT_X_L_ADDR			0x28
117 #define ST_MAGN_2_OUT_Y_L_ADDR			0x2a
118 #define ST_MAGN_2_OUT_Z_L_ADDR			0x2c
119 
120 static const struct iio_chan_spec st_magn_16bit_channels[] = {
121 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
122 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
123 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_BE, 16, 16,
124 			ST_MAGN_DEFAULT_OUT_X_H_ADDR),
125 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
126 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
127 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_BE, 16, 16,
128 			ST_MAGN_DEFAULT_OUT_Y_H_ADDR),
129 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
130 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
131 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_BE, 16, 16,
132 			ST_MAGN_DEFAULT_OUT_Z_H_ADDR),
133 	IIO_CHAN_SOFT_TIMESTAMP(3)
134 };
135 
136 static const struct iio_chan_spec st_magn_2_16bit_channels[] = {
137 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
138 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
139 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
140 			ST_MAGN_2_OUT_X_L_ADDR),
141 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
142 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
143 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
144 			ST_MAGN_2_OUT_Y_L_ADDR),
145 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
146 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
147 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
148 			ST_MAGN_2_OUT_Z_L_ADDR),
149 	IIO_CHAN_SOFT_TIMESTAMP(3)
150 };
151 
152 static const struct st_sensors st_magn_sensors[] = {
153 	{
154 		.wai = ST_MAGN_1_WAI_EXP,
155 		.sensors_supported = {
156 			[0] = LSM303DLHC_MAGN_DEV_NAME,
157 			[1] = LSM303DLM_MAGN_DEV_NAME,
158 		},
159 		.ch = (struct iio_chan_spec *)st_magn_16bit_channels,
160 		.odr = {
161 			.addr = ST_MAGN_1_ODR_ADDR,
162 			.mask = ST_MAGN_1_ODR_MASK,
163 			.odr_avl = {
164 				{ 1, ST_MAGN_1_ODR_AVL_1HZ_VAL, },
165 				{ 2, ST_MAGN_1_ODR_AVL_2HZ_VAL, },
166 				{ 3, ST_MAGN_1_ODR_AVL_3HZ_VAL, },
167 				{ 8, ST_MAGN_1_ODR_AVL_8HZ_VAL, },
168 				{ 15, ST_MAGN_1_ODR_AVL_15HZ_VAL, },
169 				{ 30, ST_MAGN_1_ODR_AVL_30HZ_VAL, },
170 				{ 75, ST_MAGN_1_ODR_AVL_75HZ_VAL, },
171 				{ 220, ST_MAGN_1_ODR_AVL_220HZ_VAL, },
172 			},
173 		},
174 		.pw = {
175 			.addr = ST_MAGN_1_PW_ADDR,
176 			.mask = ST_MAGN_1_PW_MASK,
177 			.value_on = ST_MAGN_1_PW_ON,
178 			.value_off = ST_MAGN_1_PW_OFF,
179 		},
180 		.fs = {
181 			.addr = ST_MAGN_1_FS_ADDR,
182 			.mask = ST_MAGN_1_FS_MASK,
183 			.fs_avl = {
184 				[0] = {
185 					.num = ST_MAGN_FS_AVL_1300MG,
186 					.value = ST_MAGN_1_FS_AVL_1300_VAL,
187 					.gain = ST_MAGN_1_FS_AVL_1300_GAIN_XY,
188 					.gain2 = ST_MAGN_1_FS_AVL_1300_GAIN_Z,
189 				},
190 				[1] = {
191 					.num = ST_MAGN_FS_AVL_1900MG,
192 					.value = ST_MAGN_1_FS_AVL_1900_VAL,
193 					.gain = ST_MAGN_1_FS_AVL_1900_GAIN_XY,
194 					.gain2 = ST_MAGN_1_FS_AVL_1900_GAIN_Z,
195 				},
196 				[2] = {
197 					.num = ST_MAGN_FS_AVL_2500MG,
198 					.value = ST_MAGN_1_FS_AVL_2500_VAL,
199 					.gain = ST_MAGN_1_FS_AVL_2500_GAIN_XY,
200 					.gain2 = ST_MAGN_1_FS_AVL_2500_GAIN_Z,
201 				},
202 				[3] = {
203 					.num = ST_MAGN_FS_AVL_4000MG,
204 					.value = ST_MAGN_1_FS_AVL_4000_VAL,
205 					.gain = ST_MAGN_1_FS_AVL_4000_GAIN_XY,
206 					.gain2 = ST_MAGN_1_FS_AVL_4000_GAIN_Z,
207 				},
208 				[4] = {
209 					.num = ST_MAGN_FS_AVL_4700MG,
210 					.value = ST_MAGN_1_FS_AVL_4700_VAL,
211 					.gain = ST_MAGN_1_FS_AVL_4700_GAIN_XY,
212 					.gain2 = ST_MAGN_1_FS_AVL_4700_GAIN_Z,
213 				},
214 				[5] = {
215 					.num = ST_MAGN_FS_AVL_5600MG,
216 					.value = ST_MAGN_1_FS_AVL_5600_VAL,
217 					.gain = ST_MAGN_1_FS_AVL_5600_GAIN_XY,
218 					.gain2 = ST_MAGN_1_FS_AVL_5600_GAIN_Z,
219 				},
220 				[6] = {
221 					.num = ST_MAGN_FS_AVL_8100MG,
222 					.value = ST_MAGN_1_FS_AVL_8100_VAL,
223 					.gain = ST_MAGN_1_FS_AVL_8100_GAIN_XY,
224 					.gain2 = ST_MAGN_1_FS_AVL_8100_GAIN_Z,
225 				},
226 			},
227 		},
228 		.multi_read_bit = ST_MAGN_1_MULTIREAD_BIT,
229 		.bootime = 2,
230 	},
231 	{
232 		.wai = ST_MAGN_2_WAI_EXP,
233 		.sensors_supported = {
234 			[0] = LIS3MDL_MAGN_DEV_NAME,
235 		},
236 		.ch = (struct iio_chan_spec *)st_magn_2_16bit_channels,
237 		.odr = {
238 			.addr = ST_MAGN_2_ODR_ADDR,
239 			.mask = ST_MAGN_2_ODR_MASK,
240 			.odr_avl = {
241 				{ 1, ST_MAGN_2_ODR_AVL_1HZ_VAL, },
242 				{ 2, ST_MAGN_2_ODR_AVL_2HZ_VAL, },
243 				{ 3, ST_MAGN_2_ODR_AVL_3HZ_VAL, },
244 				{ 5, ST_MAGN_2_ODR_AVL_5HZ_VAL, },
245 				{ 10, ST_MAGN_2_ODR_AVL_10HZ_VAL, },
246 				{ 20, ST_MAGN_2_ODR_AVL_20HZ_VAL, },
247 				{ 40, ST_MAGN_2_ODR_AVL_40HZ_VAL, },
248 				{ 80, ST_MAGN_2_ODR_AVL_80HZ_VAL, },
249 			},
250 		},
251 		.pw = {
252 			.addr = ST_MAGN_2_PW_ADDR,
253 			.mask = ST_MAGN_2_PW_MASK,
254 			.value_on = ST_MAGN_2_PW_ON,
255 			.value_off = ST_MAGN_2_PW_OFF,
256 		},
257 		.fs = {
258 			.addr = ST_MAGN_2_FS_ADDR,
259 			.mask = ST_MAGN_2_FS_MASK,
260 			.fs_avl = {
261 				[0] = {
262 					.num = ST_MAGN_FS_AVL_4000MG,
263 					.value = ST_MAGN_2_FS_AVL_4000_VAL,
264 					.gain = ST_MAGN_2_FS_AVL_4000_GAIN,
265 				},
266 				[1] = {
267 					.num = ST_MAGN_FS_AVL_8000MG,
268 					.value = ST_MAGN_2_FS_AVL_8000_VAL,
269 					.gain = ST_MAGN_2_FS_AVL_8000_GAIN,
270 				},
271 				[2] = {
272 					.num = ST_MAGN_FS_AVL_12000MG,
273 					.value = ST_MAGN_2_FS_AVL_12000_VAL,
274 					.gain = ST_MAGN_2_FS_AVL_12000_GAIN,
275 				},
276 				[3] = {
277 					.num = ST_MAGN_FS_AVL_16000MG,
278 					.value = ST_MAGN_2_FS_AVL_16000_VAL,
279 					.gain = ST_MAGN_2_FS_AVL_16000_GAIN,
280 				},
281 			},
282 		},
283 		.multi_read_bit = ST_MAGN_2_MULTIREAD_BIT,
284 		.bootime = 2,
285 	},
286 };
287 
st_magn_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * ch,int * val,int * val2,long mask)288 static int st_magn_read_raw(struct iio_dev *indio_dev,
289 			struct iio_chan_spec const *ch, int *val,
290 							int *val2, long mask)
291 {
292 	int err;
293 	struct st_sensor_data *mdata = iio_priv(indio_dev);
294 
295 	switch (mask) {
296 	case IIO_CHAN_INFO_RAW:
297 		err = st_sensors_read_info_raw(indio_dev, ch, val);
298 		if (err < 0)
299 			goto read_error;
300 
301 		return IIO_VAL_INT;
302 	case IIO_CHAN_INFO_SCALE:
303 		*val = 0;
304 		if ((ch->scan_index == ST_SENSORS_SCAN_Z) &&
305 					(mdata->current_fullscale->gain2 != 0))
306 			*val2 = mdata->current_fullscale->gain2;
307 		else
308 			*val2 = mdata->current_fullscale->gain;
309 		return IIO_VAL_INT_PLUS_MICRO;
310 	case IIO_CHAN_INFO_SAMP_FREQ:
311 		*val = mdata->odr;
312 		return IIO_VAL_INT;
313 	default:
314 		return -EINVAL;
315 	}
316 
317 read_error:
318 	return err;
319 }
320 
st_magn_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)321 static int st_magn_write_raw(struct iio_dev *indio_dev,
322 		struct iio_chan_spec const *chan, int val, int val2, long mask)
323 {
324 	int err;
325 
326 	switch (mask) {
327 	case IIO_CHAN_INFO_SCALE:
328 		err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
329 		break;
330 	case IIO_CHAN_INFO_SAMP_FREQ:
331 		if (val2)
332 			return -EINVAL;
333 		mutex_lock(&indio_dev->mlock);
334 		err = st_sensors_set_odr(indio_dev, val);
335 		mutex_unlock(&indio_dev->mlock);
336 		return err;
337 	default:
338 		err = -EINVAL;
339 	}
340 
341 	return err;
342 }
343 
344 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
345 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
346 
347 static struct attribute *st_magn_attributes[] = {
348 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
349 	&iio_dev_attr_in_magn_scale_available.dev_attr.attr,
350 	NULL,
351 };
352 
353 static const struct attribute_group st_magn_attribute_group = {
354 	.attrs = st_magn_attributes,
355 };
356 
357 static const struct iio_info magn_info = {
358 	.driver_module = THIS_MODULE,
359 	.attrs = &st_magn_attribute_group,
360 	.read_raw = &st_magn_read_raw,
361 	.write_raw = &st_magn_write_raw,
362 };
363 
st_magn_common_probe(struct iio_dev * indio_dev,struct st_sensors_platform_data * pdata)364 int st_magn_common_probe(struct iio_dev *indio_dev,
365 					struct st_sensors_platform_data *pdata)
366 {
367 	struct st_sensor_data *mdata = iio_priv(indio_dev);
368 	int irq = mdata->get_irq_data_ready(indio_dev);
369 	int err;
370 
371 	indio_dev->modes = INDIO_DIRECT_MODE;
372 	indio_dev->info = &magn_info;
373 
374 	st_sensors_power_enable(indio_dev);
375 
376 	err = st_sensors_check_device_support(indio_dev,
377 				ARRAY_SIZE(st_magn_sensors), st_magn_sensors);
378 	if (err < 0)
379 		return err;
380 
381 	mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS;
382 	mdata->multiread_bit = mdata->sensor->multi_read_bit;
383 	indio_dev->channels = mdata->sensor->ch;
384 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
385 
386 	mdata->current_fullscale = (struct st_sensor_fullscale_avl *)
387 						&mdata->sensor->fs.fs_avl[0];
388 	mdata->odr = mdata->sensor->odr.odr_avl[0].hz;
389 
390 	err = st_sensors_init_sensor(indio_dev, pdata);
391 	if (err < 0)
392 		return err;
393 
394 	err = st_magn_allocate_ring(indio_dev);
395 	if (err < 0)
396 		return err;
397 
398 	if (irq > 0) {
399 		err = st_sensors_allocate_trigger(indio_dev, NULL);
400 		if (err < 0)
401 			goto st_magn_probe_trigger_error;
402 	}
403 
404 	err = iio_device_register(indio_dev);
405 	if (err)
406 		goto st_magn_device_register_error;
407 
408 	dev_info(&indio_dev->dev, "registered magnetometer %s\n",
409 		 indio_dev->name);
410 
411 	return 0;
412 
413 st_magn_device_register_error:
414 	if (irq > 0)
415 		st_sensors_deallocate_trigger(indio_dev);
416 st_magn_probe_trigger_error:
417 	st_magn_deallocate_ring(indio_dev);
418 
419 	return err;
420 }
421 EXPORT_SYMBOL(st_magn_common_probe);
422 
st_magn_common_remove(struct iio_dev * indio_dev)423 void st_magn_common_remove(struct iio_dev *indio_dev)
424 {
425 	struct st_sensor_data *mdata = iio_priv(indio_dev);
426 
427 	st_sensors_power_disable(indio_dev);
428 
429 	iio_device_unregister(indio_dev);
430 	if (mdata->get_irq_data_ready(indio_dev) > 0)
431 		st_sensors_deallocate_trigger(indio_dev);
432 
433 	st_magn_deallocate_ring(indio_dev);
434 }
435 EXPORT_SYMBOL(st_magn_common_remove);
436 
437 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
438 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
439 MODULE_LICENSE("GPL v2");
440