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