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