1 /*
2  * Device driver for the the HMC5843 multi-chip module designed
3  * for low field magnetic sensing.
4  *
5  * Copyright (C) 2010 Texas Instruments
6  *
7  * Author: Shubhrajyoti Datta <shubhrajyoti@ti.com>
8  * Acknowledgment: Jonathan Cameron <jic23@kernel.org> for valuable inputs.
9  * Support for HMC5883 and HMC5883L by Peter Meerwald <pmeerw@pmeerw.net>.
10  * Split to multiple files by Josef Gajdusek <atx@atx.name> - 2014
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  */
23 
24 #include <linux/module.h>
25 #include <linux/regmap.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include <linux/delay.h>
32 
33 #include "hmc5843.h"
34 
35 /*
36  * Range gain settings in (+-)Ga
37  * Beware: HMC5843 and HMC5883 have different recommended sensor field
38  * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
39  */
40 #define HMC5843_RANGE_GAIN_OFFSET		0x05
41 #define HMC5843_RANGE_GAIN_DEFAULT		0x01
42 #define HMC5843_RANGE_GAIN_MASK		0xe0
43 
44 /* Device status */
45 #define HMC5843_DATA_READY			0x01
46 #define HMC5843_DATA_OUTPUT_LOCK		0x02
47 
48 /* Mode register configuration */
49 #define HMC5843_MODE_CONVERSION_CONTINUOUS	0x00
50 #define HMC5843_MODE_CONVERSION_SINGLE		0x01
51 #define HMC5843_MODE_IDLE			0x02
52 #define HMC5843_MODE_SLEEP			0x03
53 #define HMC5843_MODE_MASK			0x03
54 
55 /*
56  * HMC5843: Minimum data output rate
57  * HMC5883: Typical data output rate
58  */
59 #define HMC5843_RATE_OFFSET			0x02
60 #define HMC5843_RATE_DEFAULT			0x04
61 #define HMC5843_RATE_MASK		0x1c
62 
63 /* Device measurement configuration */
64 #define HMC5843_MEAS_CONF_NORMAL		0x00
65 #define HMC5843_MEAS_CONF_POSITIVE_BIAS		0x01
66 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS		0x02
67 #define HMC5843_MEAS_CONF_MASK			0x03
68 
69 /* Scaling factors: 10000000/Gain */
70 static const int hmc5843_regval_to_nanoscale[] = {
71 	6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
72 };
73 
74 static const int hmc5883_regval_to_nanoscale[] = {
75 	7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
76 };
77 
78 static const int hmc5883l_regval_to_nanoscale[] = {
79 	7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
80 };
81 
82 /*
83  * From the datasheet:
84  * Value	| HMC5843		| HMC5883/HMC5883L
85  *		| Data output rate (Hz)	| Data output rate (Hz)
86  * 0		| 0.5			| 0.75
87  * 1		| 1			| 1.5
88  * 2		| 2			| 3
89  * 3		| 5			| 7.5
90  * 4		| 10 (default)		| 15
91  * 5		| 20			| 30
92  * 6		| 50			| 75
93  * 7		| Not used		| Not used
94  */
95 static const int hmc5843_regval_to_samp_freq[][2] = {
96 	{0, 500000}, {1, 0}, {2, 0}, {5, 0}, {10, 0}, {20, 0}, {50, 0}
97 };
98 
99 static const int hmc5883_regval_to_samp_freq[][2] = {
100 	{0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
101 	{75, 0}
102 };
103 
104 static const int hmc5983_regval_to_samp_freq[][2] = {
105 	{0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
106 	{75, 0}, {220, 0}
107 };
108 
109 /* Describe chip variants */
110 struct hmc5843_chip_info {
111 	const struct iio_chan_spec *channels;
112 	const int (*regval_to_samp_freq)[2];
113 	const int n_regval_to_samp_freq;
114 	const int *regval_to_nanoscale;
115 	const int n_regval_to_nanoscale;
116 };
117 
118 /* The lower two bits contain the current conversion mode */
hmc5843_set_mode(struct hmc5843_data * data,u8 operating_mode)119 static s32 hmc5843_set_mode(struct hmc5843_data *data, u8 operating_mode)
120 {
121 	int ret;
122 
123 	mutex_lock(&data->lock);
124 	ret = regmap_update_bits(data->regmap, HMC5843_MODE_REG,
125 				 HMC5843_MODE_MASK, operating_mode);
126 	mutex_unlock(&data->lock);
127 
128 	return ret;
129 }
130 
hmc5843_wait_measurement(struct hmc5843_data * data)131 static int hmc5843_wait_measurement(struct hmc5843_data *data)
132 {
133 	int tries = 150;
134 	unsigned int val;
135 	int ret;
136 
137 	while (tries-- > 0) {
138 		ret = regmap_read(data->regmap, HMC5843_STATUS_REG, &val);
139 		if (ret < 0)
140 			return ret;
141 		if (val & HMC5843_DATA_READY)
142 			break;
143 		msleep(20);
144 	}
145 
146 	if (tries < 0) {
147 		dev_err(data->dev, "data not ready\n");
148 		return -EIO;
149 	}
150 
151 	return 0;
152 }
153 
154 /* Return the measurement value from the specified channel */
hmc5843_read_measurement(struct hmc5843_data * data,int idx,int * val)155 static int hmc5843_read_measurement(struct hmc5843_data *data,
156 				    int idx, int *val)
157 {
158 	__be16 values[3];
159 	int ret;
160 
161 	mutex_lock(&data->lock);
162 	ret = hmc5843_wait_measurement(data);
163 	if (ret < 0) {
164 		mutex_unlock(&data->lock);
165 		return ret;
166 	}
167 	ret = regmap_bulk_read(data->regmap, HMC5843_DATA_OUT_MSB_REGS,
168 			       values, sizeof(values));
169 	mutex_unlock(&data->lock);
170 	if (ret < 0)
171 		return ret;
172 
173 	*val = sign_extend32(be16_to_cpu(values[idx]), 15);
174 	return IIO_VAL_INT;
175 }
176 
177 /*
178  * API for setting the measurement configuration to
179  * Normal, Positive bias and Negative bias
180  *
181  * From the datasheet:
182  * 0 - Normal measurement configuration (default): In normal measurement
183  *     configuration the device follows normal measurement flow. Pins BP
184  *     and BN are left floating and high impedance.
185  *
186  * 1 - Positive bias configuration: In positive bias configuration, a
187  *     positive current is forced across the resistive load on pins BP
188  *     and BN.
189  *
190  * 2 - Negative bias configuration. In negative bias configuration, a
191  *     negative current is forced across the resistive load on pins BP
192  *     and BN.
193  *
194  */
hmc5843_set_meas_conf(struct hmc5843_data * data,u8 meas_conf)195 static int hmc5843_set_meas_conf(struct hmc5843_data *data, u8 meas_conf)
196 {
197 	int ret;
198 
199 	mutex_lock(&data->lock);
200 	ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_A,
201 				 HMC5843_MEAS_CONF_MASK, meas_conf);
202 	mutex_unlock(&data->lock);
203 
204 	return ret;
205 }
206 
207 static
hmc5843_show_measurement_configuration(struct device * dev,struct device_attribute * attr,char * buf)208 ssize_t hmc5843_show_measurement_configuration(struct device *dev,
209 					       struct device_attribute *attr,
210 					       char *buf)
211 {
212 	struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
213 	unsigned int val;
214 	int ret;
215 
216 	ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &val);
217 	if (ret)
218 		return ret;
219 	val &= HMC5843_MEAS_CONF_MASK;
220 
221 	return sprintf(buf, "%d\n", val);
222 }
223 
224 static
hmc5843_set_measurement_configuration(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)225 ssize_t hmc5843_set_measurement_configuration(struct device *dev,
226 					      struct device_attribute *attr,
227 					      const char *buf,
228 					      size_t count)
229 {
230 	struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
231 	unsigned long meas_conf = 0;
232 	int ret;
233 
234 	ret = kstrtoul(buf, 10, &meas_conf);
235 	if (ret)
236 		return ret;
237 	if (meas_conf >= HMC5843_MEAS_CONF_MASK)
238 		return -EINVAL;
239 
240 	ret = hmc5843_set_meas_conf(data, meas_conf);
241 
242 	return (ret < 0) ? ret : count;
243 }
244 
245 static IIO_DEVICE_ATTR(meas_conf,
246 			S_IWUSR | S_IRUGO,
247 			hmc5843_show_measurement_configuration,
248 			hmc5843_set_measurement_configuration,
249 			0);
250 
251 static
hmc5843_show_samp_freq_avail(struct device * dev,struct device_attribute * attr,char * buf)252 ssize_t hmc5843_show_samp_freq_avail(struct device *dev,
253 				     struct device_attribute *attr, char *buf)
254 {
255 	struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
256 	size_t len = 0;
257 	int i;
258 
259 	for (i = 0; i < data->variant->n_regval_to_samp_freq; i++)
260 		len += scnprintf(buf + len, PAGE_SIZE - len,
261 			"%d.%d ", data->variant->regval_to_samp_freq[i][0],
262 			data->variant->regval_to_samp_freq[i][1]);
263 
264 	/* replace trailing space by newline */
265 	buf[len - 1] = '\n';
266 
267 	return len;
268 }
269 
270 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_samp_freq_avail);
271 
hmc5843_set_samp_freq(struct hmc5843_data * data,u8 rate)272 static int hmc5843_set_samp_freq(struct hmc5843_data *data, u8 rate)
273 {
274 	int ret;
275 
276 	mutex_lock(&data->lock);
277 	ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_A,
278 				 HMC5843_RATE_MASK,
279 				 rate << HMC5843_RATE_OFFSET);
280 	mutex_unlock(&data->lock);
281 
282 	return ret;
283 }
284 
hmc5843_get_samp_freq_index(struct hmc5843_data * data,int val,int val2)285 static int hmc5843_get_samp_freq_index(struct hmc5843_data *data,
286 				       int val, int val2)
287 {
288 	int i;
289 
290 	for (i = 0; i < data->variant->n_regval_to_samp_freq; i++)
291 		if (val == data->variant->regval_to_samp_freq[i][0] &&
292 		    val2 == data->variant->regval_to_samp_freq[i][1])
293 			return i;
294 
295 	return -EINVAL;
296 }
297 
hmc5843_set_range_gain(struct hmc5843_data * data,u8 range)298 static int hmc5843_set_range_gain(struct hmc5843_data *data, u8 range)
299 {
300 	int ret;
301 
302 	mutex_lock(&data->lock);
303 	ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_B,
304 				 HMC5843_RANGE_GAIN_MASK,
305 				 range << HMC5843_RANGE_GAIN_OFFSET);
306 	mutex_unlock(&data->lock);
307 
308 	return ret;
309 }
310 
hmc5843_show_scale_avail(struct device * dev,struct device_attribute * attr,char * buf)311 static ssize_t hmc5843_show_scale_avail(struct device *dev,
312 					struct device_attribute *attr,
313 					char *buf)
314 {
315 	struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
316 
317 	size_t len = 0;
318 	int i;
319 
320 	for (i = 0; i < data->variant->n_regval_to_nanoscale; i++)
321 		len += scnprintf(buf + len, PAGE_SIZE - len,
322 			"0.%09d ", data->variant->regval_to_nanoscale[i]);
323 
324 	/* replace trailing space by newline */
325 	buf[len - 1] = '\n';
326 
327 	return len;
328 }
329 
330 static IIO_DEVICE_ATTR(scale_available, S_IRUGO,
331 	hmc5843_show_scale_avail, NULL, 0);
332 
hmc5843_get_scale_index(struct hmc5843_data * data,int val,int val2)333 static int hmc5843_get_scale_index(struct hmc5843_data *data, int val, int val2)
334 {
335 	int i;
336 
337 	if (val)
338 		return -EINVAL;
339 
340 	for (i = 0; i < data->variant->n_regval_to_nanoscale; i++)
341 		if (val2 == data->variant->regval_to_nanoscale[i])
342 			return i;
343 
344 	return -EINVAL;
345 }
346 
hmc5843_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)347 static int hmc5843_read_raw(struct iio_dev *indio_dev,
348 			    struct iio_chan_spec const *chan,
349 			    int *val, int *val2, long mask)
350 {
351 	struct hmc5843_data *data = iio_priv(indio_dev);
352 	unsigned int rval;
353 	int ret;
354 
355 	switch (mask) {
356 	case IIO_CHAN_INFO_RAW:
357 		return hmc5843_read_measurement(data, chan->scan_index, val);
358 	case IIO_CHAN_INFO_SCALE:
359 		ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_B, &rval);
360 		if (ret < 0)
361 			return ret;
362 		rval >>= HMC5843_RANGE_GAIN_OFFSET;
363 		*val = 0;
364 		*val2 = data->variant->regval_to_nanoscale[rval];
365 		return IIO_VAL_INT_PLUS_NANO;
366 	case IIO_CHAN_INFO_SAMP_FREQ:
367 		ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &rval);
368 		if (ret < 0)
369 			return ret;
370 		rval >>= HMC5843_RATE_OFFSET;
371 		*val = data->variant->regval_to_samp_freq[rval][0];
372 		*val2 = data->variant->regval_to_samp_freq[rval][1];
373 		return IIO_VAL_INT_PLUS_MICRO;
374 	}
375 	return -EINVAL;
376 }
377 
hmc5843_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)378 static int hmc5843_write_raw(struct iio_dev *indio_dev,
379 			     struct iio_chan_spec const *chan,
380 			     int val, int val2, long mask)
381 {
382 	struct hmc5843_data *data = iio_priv(indio_dev);
383 	int rate, range;
384 
385 	switch (mask) {
386 	case IIO_CHAN_INFO_SAMP_FREQ:
387 		rate = hmc5843_get_samp_freq_index(data, val, val2);
388 		if (rate < 0)
389 			return -EINVAL;
390 
391 		return hmc5843_set_samp_freq(data, rate);
392 	case IIO_CHAN_INFO_SCALE:
393 		range = hmc5843_get_scale_index(data, val, val2);
394 		if (range < 0)
395 			return -EINVAL;
396 
397 		return hmc5843_set_range_gain(data, range);
398 	default:
399 		return -EINVAL;
400 	}
401 }
402 
hmc5843_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)403 static int hmc5843_write_raw_get_fmt(struct iio_dev *indio_dev,
404 				     struct iio_chan_spec const *chan,
405 				     long mask)
406 {
407 	switch (mask) {
408 	case IIO_CHAN_INFO_SAMP_FREQ:
409 		return IIO_VAL_INT_PLUS_MICRO;
410 	case IIO_CHAN_INFO_SCALE:
411 		return IIO_VAL_INT_PLUS_NANO;
412 	default:
413 		return -EINVAL;
414 	}
415 }
416 
hmc5843_trigger_handler(int irq,void * p)417 static irqreturn_t hmc5843_trigger_handler(int irq, void *p)
418 {
419 	struct iio_poll_func *pf = p;
420 	struct iio_dev *indio_dev = pf->indio_dev;
421 	struct hmc5843_data *data = iio_priv(indio_dev);
422 	int ret;
423 
424 	mutex_lock(&data->lock);
425 	ret = hmc5843_wait_measurement(data);
426 	if (ret < 0) {
427 		mutex_unlock(&data->lock);
428 		goto done;
429 	}
430 
431 	ret = regmap_bulk_read(data->regmap, HMC5843_DATA_OUT_MSB_REGS,
432 			       data->buffer, 3 * sizeof(__be16));
433 
434 	mutex_unlock(&data->lock);
435 	if (ret < 0)
436 		goto done;
437 
438 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
439 					   iio_get_time_ns());
440 
441 done:
442 	iio_trigger_notify_done(indio_dev->trig);
443 
444 	return IRQ_HANDLED;
445 }
446 
447 #define HMC5843_CHANNEL(axis, idx)					\
448 	{								\
449 		.type = IIO_MAGN,					\
450 		.modified = 1,						\
451 		.channel2 = IIO_MOD_##axis,				\
452 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
453 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |	\
454 			BIT(IIO_CHAN_INFO_SAMP_FREQ),			\
455 		.scan_index = idx,					\
456 		.scan_type = {						\
457 			.sign = 's',					\
458 			.realbits = 16,					\
459 			.storagebits = 16,				\
460 			.endianness = IIO_BE,				\
461 		},							\
462 	}
463 
464 static const struct iio_chan_spec hmc5843_channels[] = {
465 	HMC5843_CHANNEL(X, 0),
466 	HMC5843_CHANNEL(Y, 1),
467 	HMC5843_CHANNEL(Z, 2),
468 	IIO_CHAN_SOFT_TIMESTAMP(3),
469 };
470 
471 /* Beware: Y and Z are exchanged on HMC5883 and 5983 */
472 static const struct iio_chan_spec hmc5883_channels[] = {
473 	HMC5843_CHANNEL(X, 0),
474 	HMC5843_CHANNEL(Z, 1),
475 	HMC5843_CHANNEL(Y, 2),
476 	IIO_CHAN_SOFT_TIMESTAMP(3),
477 };
478 
479 static struct attribute *hmc5843_attributes[] = {
480 	&iio_dev_attr_meas_conf.dev_attr.attr,
481 	&iio_dev_attr_scale_available.dev_attr.attr,
482 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
483 	NULL
484 };
485 
486 static const struct attribute_group hmc5843_group = {
487 	.attrs = hmc5843_attributes,
488 };
489 
490 static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
491 	[HMC5843_ID] = {
492 		.channels = hmc5843_channels,
493 		.regval_to_samp_freq = hmc5843_regval_to_samp_freq,
494 		.n_regval_to_samp_freq =
495 				ARRAY_SIZE(hmc5843_regval_to_samp_freq),
496 		.regval_to_nanoscale = hmc5843_regval_to_nanoscale,
497 		.n_regval_to_nanoscale =
498 				ARRAY_SIZE(hmc5843_regval_to_nanoscale),
499 	},
500 	[HMC5883_ID] = {
501 		.channels = hmc5883_channels,
502 		.regval_to_samp_freq = hmc5883_regval_to_samp_freq,
503 		.n_regval_to_samp_freq =
504 				ARRAY_SIZE(hmc5883_regval_to_samp_freq),
505 		.regval_to_nanoscale = hmc5883_regval_to_nanoscale,
506 		.n_regval_to_nanoscale =
507 				ARRAY_SIZE(hmc5883_regval_to_nanoscale),
508 	},
509 	[HMC5883L_ID] = {
510 		.channels = hmc5883_channels,
511 		.regval_to_samp_freq = hmc5883_regval_to_samp_freq,
512 		.n_regval_to_samp_freq =
513 				ARRAY_SIZE(hmc5883_regval_to_samp_freq),
514 		.regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
515 		.n_regval_to_nanoscale =
516 				ARRAY_SIZE(hmc5883l_regval_to_nanoscale),
517 	},
518 	[HMC5983_ID] = {
519 		.channels = hmc5883_channels,
520 		.regval_to_samp_freq = hmc5983_regval_to_samp_freq,
521 		.n_regval_to_samp_freq =
522 				ARRAY_SIZE(hmc5983_regval_to_samp_freq),
523 		.regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
524 		.n_regval_to_nanoscale =
525 				ARRAY_SIZE(hmc5883l_regval_to_nanoscale),
526 	}
527 };
528 
hmc5843_init(struct hmc5843_data * data)529 static int hmc5843_init(struct hmc5843_data *data)
530 {
531 	int ret;
532 	u8 id[3];
533 
534 	ret = regmap_bulk_read(data->regmap, HMC5843_ID_REG,
535 			       id, ARRAY_SIZE(id));
536 	if (ret < 0)
537 		return ret;
538 	if (id[0] != 'H' || id[1] != '4' || id[2] != '3') {
539 		dev_err(data->dev, "no HMC5843/5883/5883L/5983 sensor\n");
540 		return -ENODEV;
541 	}
542 
543 	ret = hmc5843_set_meas_conf(data, HMC5843_MEAS_CONF_NORMAL);
544 	if (ret < 0)
545 		return ret;
546 	ret = hmc5843_set_samp_freq(data, HMC5843_RATE_DEFAULT);
547 	if (ret < 0)
548 		return ret;
549 	ret = hmc5843_set_range_gain(data, HMC5843_RANGE_GAIN_DEFAULT);
550 	if (ret < 0)
551 		return ret;
552 	return hmc5843_set_mode(data, HMC5843_MODE_CONVERSION_CONTINUOUS);
553 }
554 
555 static const struct iio_info hmc5843_info = {
556 	.attrs = &hmc5843_group,
557 	.read_raw = &hmc5843_read_raw,
558 	.write_raw = &hmc5843_write_raw,
559 	.write_raw_get_fmt = &hmc5843_write_raw_get_fmt,
560 	.driver_module = THIS_MODULE,
561 };
562 
563 static const unsigned long hmc5843_scan_masks[] = {0x7, 0};
564 
hmc5843_common_suspend(struct device * dev)565 int hmc5843_common_suspend(struct device *dev)
566 {
567 	return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev)),
568 			HMC5843_MODE_CONVERSION_CONTINUOUS);
569 }
570 EXPORT_SYMBOL(hmc5843_common_suspend);
571 
hmc5843_common_resume(struct device * dev)572 int hmc5843_common_resume(struct device *dev)
573 {
574 	return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev)),
575 				HMC5843_MODE_SLEEP);
576 }
577 EXPORT_SYMBOL(hmc5843_common_resume);
578 
hmc5843_common_probe(struct device * dev,struct regmap * regmap,enum hmc5843_ids id,const char * name)579 int hmc5843_common_probe(struct device *dev, struct regmap *regmap,
580 			 enum hmc5843_ids id, const char *name)
581 {
582 	struct hmc5843_data *data;
583 	struct iio_dev *indio_dev;
584 	int ret;
585 
586 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
587 	if (!indio_dev)
588 		return -ENOMEM;
589 
590 	dev_set_drvdata(dev, indio_dev);
591 
592 	/* default settings at probe */
593 	data = iio_priv(indio_dev);
594 	data->dev = dev;
595 	data->regmap = regmap;
596 	data->variant = &hmc5843_chip_info_tbl[id];
597 	mutex_init(&data->lock);
598 
599 	indio_dev->dev.parent = dev;
600 	indio_dev->name = name;
601 	indio_dev->info = &hmc5843_info;
602 	indio_dev->modes = INDIO_DIRECT_MODE;
603 	indio_dev->channels = data->variant->channels;
604 	indio_dev->num_channels = 4;
605 	indio_dev->available_scan_masks = hmc5843_scan_masks;
606 
607 	ret = hmc5843_init(data);
608 	if (ret < 0)
609 		return ret;
610 
611 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
612 					 hmc5843_trigger_handler, NULL);
613 	if (ret < 0)
614 		goto buffer_setup_err;
615 
616 	ret = iio_device_register(indio_dev);
617 	if (ret < 0)
618 		goto buffer_cleanup;
619 
620 	return 0;
621 
622 buffer_cleanup:
623 	iio_triggered_buffer_cleanup(indio_dev);
624 buffer_setup_err:
625 	hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP);
626 	return ret;
627 }
628 EXPORT_SYMBOL(hmc5843_common_probe);
629 
hmc5843_common_remove(struct device * dev)630 int hmc5843_common_remove(struct device *dev)
631 {
632 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
633 
634 	iio_device_unregister(indio_dev);
635 	iio_triggered_buffer_cleanup(indio_dev);
636 
637 	/*  sleep mode to save power */
638 	hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP);
639 
640 	return 0;
641 }
642 EXPORT_SYMBOL(hmc5843_common_remove);
643 
644 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>");
645 MODULE_DESCRIPTION("HMC5843/5883/5883L/5983 core driver");
646 MODULE_LICENSE("GPL");
647