1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * MMC35240 - MEMSIC 3-axis Magnetic Sensor
4  *
5  * Copyright (c) 2015, Intel Corporation.
6  *
7  * IIO driver for MMC35240 (7-bit I2C slave address 0x30).
8  *
9  * TODO: offset, ACPI, continuous measurement mode, PM
10  */
11 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/i2c.h>
15 #include <linux/delay.h>
16 #include <linux/regmap.h>
17 #include <linux/acpi.h>
18 #include <linux/pm.h>
19 
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 
23 #define MMC35240_DRV_NAME "mmc35240"
24 #define MMC35240_REGMAP_NAME "mmc35240_regmap"
25 
26 #define MMC35240_REG_XOUT_L	0x00
27 #define MMC35240_REG_XOUT_H	0x01
28 #define MMC35240_REG_YOUT_L	0x02
29 #define MMC35240_REG_YOUT_H	0x03
30 #define MMC35240_REG_ZOUT_L	0x04
31 #define MMC35240_REG_ZOUT_H	0x05
32 
33 #define MMC35240_REG_STATUS	0x06
34 #define MMC35240_REG_CTRL0	0x07
35 #define MMC35240_REG_CTRL1	0x08
36 
37 #define MMC35240_REG_ID		0x20
38 
39 #define MMC35240_STATUS_MEAS_DONE_BIT	BIT(0)
40 
41 #define MMC35240_CTRL0_REFILL_BIT	BIT(7)
42 #define MMC35240_CTRL0_RESET_BIT	BIT(6)
43 #define MMC35240_CTRL0_SET_BIT		BIT(5)
44 #define MMC35240_CTRL0_CMM_BIT		BIT(1)
45 #define MMC35240_CTRL0_TM_BIT		BIT(0)
46 
47 /* output resolution bits */
48 #define MMC35240_CTRL1_BW0_BIT		BIT(0)
49 #define MMC35240_CTRL1_BW1_BIT		BIT(1)
50 
51 #define MMC35240_CTRL1_BW_MASK	 (MMC35240_CTRL1_BW0_BIT | \
52 		 MMC35240_CTRL1_BW1_BIT)
53 #define MMC35240_CTRL1_BW_SHIFT		0
54 
55 #define MMC35240_WAIT_CHARGE_PUMP	50000	/* us */
56 #define MMC53240_WAIT_SET_RESET		1000	/* us */
57 
58 /*
59  * Memsic OTP process code piece is put here for reference:
60  *
61  * #define OTP_CONVERT(REG)  ((float)((REG) >=32 ? (32 - (REG)) : (REG)) * 0.006
62  * 1) For X axis, the COEFFICIENT is always 1.
63  * 2) For Y axis, the COEFFICIENT is as below:
64  *    f_OTP_matrix[4] = OTP_CONVERT(((reg_data[1] & 0x03) << 4) |
65  *                                   (reg_data[2] >> 4)) + 1.0;
66  * 3) For Z axis, the COEFFICIENT is as below:
67  *    f_OTP_matrix[8] = (OTP_CONVERT(reg_data[3] & 0x3f) + 1) * 1.35;
68  * We implemented the OTP logic into driver.
69  */
70 
71 /* scale = 1000 here for Y otp */
72 #define MMC35240_OTP_CONVERT_Y(REG) (((REG) >= 32 ? (32 - (REG)) : (REG)) * 6)
73 
74 /* 0.6 * 1.35 = 0.81, scale 10000 for Z otp */
75 #define MMC35240_OTP_CONVERT_Z(REG) (((REG) >= 32 ? (32 - (REG)) : (REG)) * 81)
76 
77 #define MMC35240_X_COEFF(x)	(x)
78 #define MMC35240_Y_COEFF(y)	(y + 1000)
79 #define MMC35240_Z_COEFF(z)	(z + 13500)
80 
81 #define MMC35240_OTP_START_ADDR		0x1B
82 
83 enum mmc35240_resolution {
84 	MMC35240_16_BITS_SLOW = 0, /* 7.92 ms */
85 	MMC35240_16_BITS_FAST,     /* 4.08 ms */
86 	MMC35240_14_BITS,          /* 2.16 ms */
87 	MMC35240_12_BITS,          /* 1.20 ms */
88 };
89 
90 enum mmc35240_axis {
91 	AXIS_X = 0,
92 	AXIS_Y,
93 	AXIS_Z,
94 };
95 
96 static const struct {
97 	int sens[3]; /* sensitivity per X, Y, Z axis */
98 	int nfo; /* null field output */
99 } mmc35240_props_table[] = {
100 	/* 16 bits, 125Hz ODR */
101 	{
102 		{1024, 1024, 1024},
103 		32768,
104 	},
105 	/* 16 bits, 250Hz ODR */
106 	{
107 		{1024, 1024, 770},
108 		32768,
109 	},
110 	/* 14 bits, 450Hz ODR */
111 	{
112 		{256, 256, 193},
113 		8192,
114 	},
115 	/* 12 bits, 800Hz ODR */
116 	{
117 		{64, 64, 48},
118 		2048,
119 	},
120 };
121 
122 struct mmc35240_data {
123 	struct i2c_client *client;
124 	struct mutex mutex;
125 	struct regmap *regmap;
126 	enum mmc35240_resolution res;
127 
128 	/* OTP compensation */
129 	int axis_coef[3];
130 	int axis_scale[3];
131 };
132 
133 static const struct {
134 	int val;
135 	int val2;
136 } mmc35240_samp_freq[] = { {1, 500000},
137 			   {13, 0},
138 			   {25, 0},
139 			   {50, 0} };
140 
141 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1.5 13 25 50");
142 
143 #define MMC35240_CHANNEL(_axis) { \
144 	.type = IIO_MAGN, \
145 	.modified = 1, \
146 	.channel2 = IIO_MOD_ ## _axis, \
147 	.address = AXIS_ ## _axis, \
148 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
149 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
150 			BIT(IIO_CHAN_INFO_SCALE), \
151 }
152 
153 static const struct iio_chan_spec mmc35240_channels[] = {
154 	MMC35240_CHANNEL(X),
155 	MMC35240_CHANNEL(Y),
156 	MMC35240_CHANNEL(Z),
157 };
158 
159 static struct attribute *mmc35240_attributes[] = {
160 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
161 	NULL
162 };
163 
164 static const struct attribute_group mmc35240_attribute_group = {
165 	.attrs = mmc35240_attributes,
166 };
167 
168 static int mmc35240_get_samp_freq_index(struct mmc35240_data *data,
169 					int val, int val2)
170 {
171 	int i;
172 
173 	for (i = 0; i < ARRAY_SIZE(mmc35240_samp_freq); i++)
174 		if (mmc35240_samp_freq[i].val == val &&
175 		    mmc35240_samp_freq[i].val2 == val2)
176 			return i;
177 	return -EINVAL;
178 }
179 
180 static int mmc35240_hw_set(struct mmc35240_data *data, bool set)
181 {
182 	int ret;
183 	u8 coil_bit;
184 
185 	/*
186 	 * Recharge the capacitor at VCAP pin, requested to be issued
187 	 * before a SET/RESET command.
188 	 */
189 	ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL0,
190 				 MMC35240_CTRL0_REFILL_BIT,
191 				 MMC35240_CTRL0_REFILL_BIT);
192 	if (ret < 0)
193 		return ret;
194 	usleep_range(MMC35240_WAIT_CHARGE_PUMP, MMC35240_WAIT_CHARGE_PUMP + 1);
195 
196 	if (set)
197 		coil_bit = MMC35240_CTRL0_SET_BIT;
198 	else
199 		coil_bit = MMC35240_CTRL0_RESET_BIT;
200 
201 	return regmap_update_bits(data->regmap, MMC35240_REG_CTRL0,
202 				  coil_bit, coil_bit);
203 
204 }
205 
206 static int mmc35240_init(struct mmc35240_data *data)
207 {
208 	int ret, y_convert, z_convert;
209 	unsigned int reg_id;
210 	u8 otp_data[6];
211 
212 	ret = regmap_read(data->regmap, MMC35240_REG_ID, &reg_id);
213 	if (ret < 0) {
214 		dev_err(&data->client->dev, "Error reading product id\n");
215 		return ret;
216 	}
217 
218 	dev_dbg(&data->client->dev, "MMC35240 chip id %x\n", reg_id);
219 
220 	/*
221 	 * make sure we restore sensor characteristics, by doing
222 	 * a SET/RESET sequence, the axis polarity being naturally
223 	 * aligned after RESET
224 	 */
225 	ret = mmc35240_hw_set(data, true);
226 	if (ret < 0)
227 		return ret;
228 	usleep_range(MMC53240_WAIT_SET_RESET, MMC53240_WAIT_SET_RESET + 1);
229 
230 	ret = mmc35240_hw_set(data, false);
231 	if (ret < 0)
232 		return ret;
233 
234 	/* set default sampling frequency */
235 	ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1,
236 				 MMC35240_CTRL1_BW_MASK,
237 				 data->res << MMC35240_CTRL1_BW_SHIFT);
238 	if (ret < 0)
239 		return ret;
240 
241 	ret = regmap_bulk_read(data->regmap, MMC35240_OTP_START_ADDR,
242 			       (u8 *)otp_data, sizeof(otp_data));
243 	if (ret < 0)
244 		return ret;
245 
246 	y_convert = MMC35240_OTP_CONVERT_Y(((otp_data[1] & 0x03) << 4) |
247 					   (otp_data[2] >> 4));
248 	z_convert = MMC35240_OTP_CONVERT_Z(otp_data[3] & 0x3f);
249 
250 	data->axis_coef[0] = MMC35240_X_COEFF(1);
251 	data->axis_coef[1] = MMC35240_Y_COEFF(y_convert);
252 	data->axis_coef[2] = MMC35240_Z_COEFF(z_convert);
253 
254 	data->axis_scale[0] = 1;
255 	data->axis_scale[1] = 1000;
256 	data->axis_scale[2] = 10000;
257 
258 	return 0;
259 }
260 
261 static int mmc35240_take_measurement(struct mmc35240_data *data)
262 {
263 	int ret, tries = 100;
264 	unsigned int reg_status;
265 
266 	ret = regmap_write(data->regmap, MMC35240_REG_CTRL0,
267 			   MMC35240_CTRL0_TM_BIT);
268 	if (ret < 0)
269 		return ret;
270 
271 	while (tries-- > 0) {
272 		ret = regmap_read(data->regmap, MMC35240_REG_STATUS,
273 				  &reg_status);
274 		if (ret < 0)
275 			return ret;
276 		if (reg_status & MMC35240_STATUS_MEAS_DONE_BIT)
277 			break;
278 		/* minimum wait time to complete measurement is 10 ms */
279 		usleep_range(10000, 11000);
280 	}
281 
282 	if (tries < 0) {
283 		dev_err(&data->client->dev, "data not ready\n");
284 		return -EIO;
285 	}
286 
287 	return 0;
288 }
289 
290 static int mmc35240_read_measurement(struct mmc35240_data *data, __le16 buf[3])
291 {
292 	int ret;
293 
294 	ret = mmc35240_take_measurement(data);
295 	if (ret < 0)
296 		return ret;
297 
298 	return regmap_bulk_read(data->regmap, MMC35240_REG_XOUT_L, (u8 *)buf,
299 				3 * sizeof(__le16));
300 }
301 
302 /**
303  * mmc35240_raw_to_mgauss - convert raw readings to milli gauss. Also apply
304 			    compensation for output value.
305  *
306  * @data: device private data
307  * @index: axis index for which we want the conversion
308  * @buf: raw data to be converted, 2 bytes in little endian format
309  * @val: compensated output reading (unit is milli gauss)
310  *
311  * Returns: 0 in case of success, -EINVAL when @index is not valid
312  */
313 static int mmc35240_raw_to_mgauss(struct mmc35240_data *data, int index,
314 				  __le16 buf[], int *val)
315 {
316 	int raw[3];
317 	int sens[3];
318 	int nfo;
319 
320 	raw[AXIS_X] = le16_to_cpu(buf[AXIS_X]);
321 	raw[AXIS_Y] = le16_to_cpu(buf[AXIS_Y]);
322 	raw[AXIS_Z] = le16_to_cpu(buf[AXIS_Z]);
323 
324 	sens[AXIS_X] = mmc35240_props_table[data->res].sens[AXIS_X];
325 	sens[AXIS_Y] = mmc35240_props_table[data->res].sens[AXIS_Y];
326 	sens[AXIS_Z] = mmc35240_props_table[data->res].sens[AXIS_Z];
327 
328 	nfo = mmc35240_props_table[data->res].nfo;
329 
330 	switch (index) {
331 	case AXIS_X:
332 		*val = (raw[AXIS_X] - nfo) * 1000 / sens[AXIS_X];
333 		break;
334 	case AXIS_Y:
335 		*val = (raw[AXIS_Y] - nfo) * 1000 / sens[AXIS_Y] -
336 			(raw[AXIS_Z] - nfo)  * 1000 / sens[AXIS_Z];
337 		break;
338 	case AXIS_Z:
339 		*val = (raw[AXIS_Y] - nfo) * 1000 / sens[AXIS_Y] +
340 			(raw[AXIS_Z] - nfo) * 1000 / sens[AXIS_Z];
341 		break;
342 	default:
343 		return -EINVAL;
344 	}
345 	/* apply OTP compensation */
346 	*val = (*val) * data->axis_coef[index] / data->axis_scale[index];
347 
348 	return 0;
349 }
350 
351 static int mmc35240_read_raw(struct iio_dev *indio_dev,
352 			     struct iio_chan_spec const *chan, int *val,
353 			     int *val2, long mask)
354 {
355 	struct mmc35240_data *data = iio_priv(indio_dev);
356 	int ret, i;
357 	unsigned int reg;
358 	__le16 buf[3];
359 
360 	switch (mask) {
361 	case IIO_CHAN_INFO_RAW:
362 		mutex_lock(&data->mutex);
363 		ret = mmc35240_read_measurement(data, buf);
364 		mutex_unlock(&data->mutex);
365 		if (ret < 0)
366 			return ret;
367 		ret = mmc35240_raw_to_mgauss(data, chan->address, buf, val);
368 		if (ret < 0)
369 			return ret;
370 		return IIO_VAL_INT;
371 	case IIO_CHAN_INFO_SCALE:
372 		*val = 0;
373 		*val2 = 1000;
374 		return IIO_VAL_INT_PLUS_MICRO;
375 	case IIO_CHAN_INFO_SAMP_FREQ:
376 		mutex_lock(&data->mutex);
377 		ret = regmap_read(data->regmap, MMC35240_REG_CTRL1, &reg);
378 		mutex_unlock(&data->mutex);
379 		if (ret < 0)
380 			return ret;
381 
382 		i = (reg & MMC35240_CTRL1_BW_MASK) >> MMC35240_CTRL1_BW_SHIFT;
383 		if (i < 0 || i >= ARRAY_SIZE(mmc35240_samp_freq))
384 			return -EINVAL;
385 
386 		*val = mmc35240_samp_freq[i].val;
387 		*val2 = mmc35240_samp_freq[i].val2;
388 		return IIO_VAL_INT_PLUS_MICRO;
389 	default:
390 		return -EINVAL;
391 	}
392 }
393 
394 static int mmc35240_write_raw(struct iio_dev *indio_dev,
395 			      struct iio_chan_spec const *chan, int val,
396 			      int val2, long mask)
397 {
398 	struct mmc35240_data *data = iio_priv(indio_dev);
399 	int i, ret;
400 
401 	switch (mask) {
402 	case IIO_CHAN_INFO_SAMP_FREQ:
403 		i = mmc35240_get_samp_freq_index(data, val, val2);
404 		if (i < 0)
405 			return -EINVAL;
406 		mutex_lock(&data->mutex);
407 		ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1,
408 					 MMC35240_CTRL1_BW_MASK,
409 					 i << MMC35240_CTRL1_BW_SHIFT);
410 		mutex_unlock(&data->mutex);
411 		return ret;
412 	default:
413 		return -EINVAL;
414 	}
415 }
416 
417 static const struct iio_info mmc35240_info = {
418 	.read_raw	= mmc35240_read_raw,
419 	.write_raw	= mmc35240_write_raw,
420 	.attrs		= &mmc35240_attribute_group,
421 };
422 
423 static bool mmc35240_is_writeable_reg(struct device *dev, unsigned int reg)
424 {
425 	switch (reg) {
426 	case MMC35240_REG_CTRL0:
427 	case MMC35240_REG_CTRL1:
428 		return true;
429 	default:
430 		return false;
431 	}
432 }
433 
434 static bool mmc35240_is_readable_reg(struct device *dev, unsigned int reg)
435 {
436 	switch (reg) {
437 	case MMC35240_REG_XOUT_L:
438 	case MMC35240_REG_XOUT_H:
439 	case MMC35240_REG_YOUT_L:
440 	case MMC35240_REG_YOUT_H:
441 	case MMC35240_REG_ZOUT_L:
442 	case MMC35240_REG_ZOUT_H:
443 	case MMC35240_REG_STATUS:
444 	case MMC35240_REG_ID:
445 		return true;
446 	default:
447 		return false;
448 	}
449 }
450 
451 static bool mmc35240_is_volatile_reg(struct device *dev, unsigned int reg)
452 {
453 	switch (reg) {
454 	case MMC35240_REG_CTRL0:
455 	case MMC35240_REG_CTRL1:
456 		return false;
457 	default:
458 		return true;
459 	}
460 }
461 
462 static struct reg_default mmc35240_reg_defaults[] = {
463 	{ MMC35240_REG_CTRL0,  0x00 },
464 	{ MMC35240_REG_CTRL1,  0x00 },
465 };
466 
467 static const struct regmap_config mmc35240_regmap_config = {
468 	.name = MMC35240_REGMAP_NAME,
469 
470 	.reg_bits = 8,
471 	.val_bits = 8,
472 
473 	.max_register = MMC35240_REG_ID,
474 	.cache_type = REGCACHE_FLAT,
475 
476 	.writeable_reg = mmc35240_is_writeable_reg,
477 	.readable_reg = mmc35240_is_readable_reg,
478 	.volatile_reg = mmc35240_is_volatile_reg,
479 
480 	.reg_defaults = mmc35240_reg_defaults,
481 	.num_reg_defaults = ARRAY_SIZE(mmc35240_reg_defaults),
482 };
483 
484 static int mmc35240_probe(struct i2c_client *client,
485 			  const struct i2c_device_id *id)
486 {
487 	struct mmc35240_data *data;
488 	struct iio_dev *indio_dev;
489 	struct regmap *regmap;
490 	int ret;
491 
492 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
493 	if (!indio_dev)
494 		return -ENOMEM;
495 
496 	regmap = devm_regmap_init_i2c(client, &mmc35240_regmap_config);
497 	if (IS_ERR(regmap)) {
498 		dev_err(&client->dev, "regmap initialization failed\n");
499 		return PTR_ERR(regmap);
500 	}
501 
502 	data = iio_priv(indio_dev);
503 	i2c_set_clientdata(client, indio_dev);
504 	data->client = client;
505 	data->regmap = regmap;
506 	data->res = MMC35240_16_BITS_SLOW;
507 
508 	mutex_init(&data->mutex);
509 
510 	indio_dev->dev.parent = &client->dev;
511 	indio_dev->info = &mmc35240_info;
512 	indio_dev->name = MMC35240_DRV_NAME;
513 	indio_dev->channels = mmc35240_channels;
514 	indio_dev->num_channels = ARRAY_SIZE(mmc35240_channels);
515 	indio_dev->modes = INDIO_DIRECT_MODE;
516 
517 	ret = mmc35240_init(data);
518 	if (ret < 0) {
519 		dev_err(&client->dev, "mmc35240 chip init failed\n");
520 		return ret;
521 	}
522 	return devm_iio_device_register(&client->dev, indio_dev);
523 }
524 
525 #ifdef CONFIG_PM_SLEEP
526 static int mmc35240_suspend(struct device *dev)
527 {
528 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
529 	struct mmc35240_data *data = iio_priv(indio_dev);
530 
531 	regcache_cache_only(data->regmap, true);
532 
533 	return 0;
534 }
535 
536 static int mmc35240_resume(struct device *dev)
537 {
538 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
539 	struct mmc35240_data *data = iio_priv(indio_dev);
540 	int ret;
541 
542 	regcache_mark_dirty(data->regmap);
543 	ret = regcache_sync_region(data->regmap, MMC35240_REG_CTRL0,
544 				   MMC35240_REG_CTRL1);
545 	if (ret < 0)
546 		dev_err(dev, "Failed to restore control registers\n");
547 
548 	regcache_cache_only(data->regmap, false);
549 
550 	return 0;
551 }
552 #endif
553 
554 static const struct dev_pm_ops mmc35240_pm_ops = {
555 	SET_SYSTEM_SLEEP_PM_OPS(mmc35240_suspend, mmc35240_resume)
556 };
557 
558 static const struct of_device_id mmc35240_of_match[] = {
559 	{ .compatible = "memsic,mmc35240", },
560 	{ }
561 };
562 MODULE_DEVICE_TABLE(of, mmc35240_of_match);
563 
564 static const struct acpi_device_id mmc35240_acpi_match[] = {
565 	{"MMC35240", 0},
566 	{ },
567 };
568 MODULE_DEVICE_TABLE(acpi, mmc35240_acpi_match);
569 
570 static const struct i2c_device_id mmc35240_id[] = {
571 	{"mmc35240", 0},
572 	{}
573 };
574 MODULE_DEVICE_TABLE(i2c, mmc35240_id);
575 
576 static struct i2c_driver mmc35240_driver = {
577 	.driver = {
578 		.name = MMC35240_DRV_NAME,
579 		.of_match_table = mmc35240_of_match,
580 		.pm = &mmc35240_pm_ops,
581 		.acpi_match_table = ACPI_PTR(mmc35240_acpi_match),
582 	},
583 	.probe		= mmc35240_probe,
584 	.id_table	= mmc35240_id,
585 };
586 
587 module_i2c_driver(mmc35240_driver);
588 
589 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
590 MODULE_DESCRIPTION("MEMSIC MMC35240 magnetic sensor driver");
591 MODULE_LICENSE("GPL v2");
592