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