]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/magnetometer/hmc5843.c
Merge branches 'fixes', 'misc', 'mmci', 'unstable/dma-for-next' and 'sa11x0' into...
[karo-tx-linux.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/delay.h>
27
28 #define HMC5843_CONFIG_REG_A                    0x00
29 #define HMC5843_CONFIG_REG_B                    0x01
30 #define HMC5843_MODE_REG                        0x02
31 #define HMC5843_DATA_OUT_X_MSB_REG              0x03
32 #define HMC5843_DATA_OUT_X_LSB_REG              0x04
33 #define HMC5843_DATA_OUT_Y_MSB_REG              0x05
34 #define HMC5843_DATA_OUT_Y_LSB_REG              0x06
35 #define HMC5843_DATA_OUT_Z_MSB_REG              0x07
36 #define HMC5843_DATA_OUT_Z_LSB_REG              0x08
37 /* Beware: Y and Z are exchanged on HMC5883 */
38 #define HMC5883_DATA_OUT_Z_MSB_REG              0x05
39 #define HMC5883_DATA_OUT_Z_LSB_REG              0x06
40 #define HMC5883_DATA_OUT_Y_MSB_REG              0x07
41 #define HMC5883_DATA_OUT_Y_LSB_REG              0x08
42 #define HMC5843_STATUS_REG                      0x09
43
44 enum hmc5843_ids {
45         HMC5843_ID,
46         HMC5883_ID,
47         HMC5883L_ID,
48 };
49
50 /*
51  * Range gain settings in (+-)Ga
52  * Beware: HMC5843 and HMC5883 have different recommended sensor field
53  * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
54  */
55 #define HMC5843_RANGE_GAIN_OFFSET               0x05
56 #define HMC5843_RANGE_GAIN_DEFAULT              0x01
57 #define HMC5843_RANGE_GAIN_MAX                  0x07
58
59 /*
60  * Device status
61  */
62 #define HMC5843_DATA_READY                      0x01
63 #define HMC5843_DATA_OUTPUT_LOCK                0x02
64 /* Does not exist on HMC5883, not used */
65 #define HMC5843_VOLTAGE_REGULATOR_ENABLED       0x04
66
67 /*
68  * Mode register configuration
69  */
70 #define HMC5843_MODE_CONVERSION_CONTINUOUS      0x00
71 #define HMC5843_MODE_CONVERSION_SINGLE          0x01
72 #define HMC5843_MODE_IDLE                       0x02
73 #define HMC5843_MODE_SLEEP                      0x03
74 #define HMC5843_MODE_MASK                       0x03
75
76 /*
77  * HMC5843: Minimum data output rate
78  * HMC5883: Typical data output rate
79  */
80 #define HMC5843_RATE_OFFSET                     0x02
81 #define HMC5843_RATE_BITMASK                    0x1C
82 #define HMC5843_RATE_NOT_USED                   0x07
83
84 /*
85  * Device measurement configuration
86  */
87 #define HMC5843_MEAS_CONF_NORMAL                0x00
88 #define HMC5843_MEAS_CONF_POSITIVE_BIAS         0x01
89 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS         0x02
90 #define HMC5843_MEAS_CONF_NOT_USED              0x03
91 #define HMC5843_MEAS_CONF_MASK                  0x03
92
93 /*
94  * Scaling factors: 10000000/Gain
95  */
96 static const int hmc5843_regval_to_nanoscale[] = {
97         6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
98 };
99
100 static const int hmc5883_regval_to_nanoscale[] = {
101         7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
102 };
103
104 static const int hmc5883l_regval_to_nanoscale[] = {
105         7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
106 };
107
108 /*
109  * From the HMC5843 datasheet:
110  * Value        | Sensor input field range (Ga) | Gain (counts/milli-Gauss)
111  * 0            | (+-)0.7                       | 1620
112  * 1            | (+-)1.0                       | 1300
113  * 2            | (+-)1.5                       | 970
114  * 3            | (+-)2.0                       | 780
115  * 4            | (+-)3.2                       | 530
116  * 5            | (+-)3.8                       | 460
117  * 6            | (+-)4.5                       | 390
118  * 7            | (+-)6.5                       | 280
119  *
120  * From the HMC5883 datasheet:
121  * Value        | Recommended sensor field range (Ga)   | Gain (counts/Gauss)
122  * 0            | (+-)0.9                               | 1280
123  * 1            | (+-)1.2                               | 1024
124  * 2            | (+-)1.9                               | 768
125  * 3            | (+-)2.5                               | 614
126  * 4            | (+-)4.0                               | 415
127  * 5            | (+-)4.6                               | 361
128  * 6            | (+-)5.5                               | 307
129  * 7            | (+-)7.9                               | 219
130  *
131  * From the HMC5883L datasheet:
132  * Value        | Recommended sensor field range (Ga)   | Gain (LSB/Gauss)
133  * 0            | (+-)0.88                              | 1370
134  * 1            | (+-)1.3                               | 1090
135  * 2            | (+-)1.9                               | 820
136  * 3            | (+-)2.5                               | 660
137  * 4            | (+-)4.0                               | 440
138  * 5            | (+-)4.7                               | 390
139  * 6            | (+-)5.6                               | 330
140  * 7            | (+-)8.1                               | 230
141  */
142 static const int hmc5843_regval_to_input_field_mga[] = {
143         700, 1000, 1500, 2000, 3200, 3800, 4500, 6500
144 };
145
146 static const int hmc5883_regval_to_input_field_mga[] = {
147         900, 1200, 1900, 2500, 4000, 4600, 5500, 7900
148 };
149
150 static const int hmc5883l_regval_to_input_field_mga[] = {
151         880, 1300, 1900, 2500, 4000, 4700, 5600, 8100
152 };
153
154 /*
155  * From the datasheet:
156  * Value        | HMC5843               | HMC5883/HMC5883L
157  *              | Data output rate (Hz) | Data output rate (Hz)
158  * 0            | 0.5                   | 0.75
159  * 1            | 1                     | 1.5
160  * 2            | 2                     | 3
161  * 3            | 5                     | 7.5
162  * 4            | 10 (default)          | 15
163  * 5            | 20                    | 30
164  * 6            | 50                    | 75
165  * 7            | Not used              | Not used
166  */
167 static const char * const hmc5843_regval_to_sample_freq[] = {
168         "0.5", "1", "2", "5", "10", "20", "50",
169 };
170
171 static const char * const hmc5883_regval_to_sample_freq[] = {
172         "0.75", "1.5", "3", "7.5", "15", "30", "75",
173 };
174
175 /* Describe chip variants */
176 struct hmc5843_chip_info {
177         const struct iio_chan_spec *channels;
178         const char * const *regval_to_sample_freq;
179         const int *regval_to_input_field_mga;
180         const int *regval_to_nanoscale;
181 };
182
183 /* Each client has this additional data */
184 struct hmc5843_data {
185         struct mutex lock;
186         u8 rate;
187         u8 meas_conf;
188         u8 operating_mode;
189         u8 range;
190         const struct hmc5843_chip_info *variant;
191 };
192
193 /* The lower two bits contain the current conversion mode */
194 static s32 hmc5843_configure(struct i2c_client *client,
195                                        u8 operating_mode)
196 {
197         return i2c_smbus_write_byte_data(client,
198                                         HMC5843_MODE_REG,
199                                         operating_mode & HMC5843_MODE_MASK);
200 }
201
202 /* Return the measurement value from the specified channel */
203 static int hmc5843_read_measurement(struct iio_dev *indio_dev,
204                                     int address,
205                                     int *val)
206 {
207         struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
208         struct hmc5843_data *data = iio_priv(indio_dev);
209         s32 result;
210         int tries = 150;
211
212         mutex_lock(&data->lock);
213         while (tries-- > 0) {
214                 result = i2c_smbus_read_byte_data(client,
215                         HMC5843_STATUS_REG);
216                 if (result & HMC5843_DATA_READY)
217                         break;
218                 msleep(20);
219         }
220
221         if (tries < 0) {
222                 dev_err(&client->dev, "data not ready\n");
223                 mutex_unlock(&data->lock);
224                 return -EIO;
225         }
226
227         result = i2c_smbus_read_word_swapped(client, address);
228         mutex_unlock(&data->lock);
229         if (result < 0)
230                 return -EINVAL;
231
232         *val = sign_extend32(result, 15);
233         return IIO_VAL_INT;
234 }
235
236 /*
237  * From the datasheet:
238  * 0 - Continuous-Conversion Mode: In continuous-conversion mode, the
239  *     device continuously performs conversions and places the result in
240  *     the data register.
241  *
242  * 1 - Single-Conversion Mode : Device performs a single measurement,
243  *     sets RDY high and returns to sleep mode.
244  *
245  * 2 - Idle Mode : Device is placed in idle mode.
246  *
247  * 3 - Sleep Mode : Device is placed in sleep mode.
248  *
249  */
250 static ssize_t hmc5843_show_operating_mode(struct device *dev,
251                                         struct device_attribute *attr,
252                                         char *buf)
253 {
254         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
255         struct hmc5843_data *data = iio_priv(indio_dev);
256         return sprintf(buf, "%d\n", data->operating_mode);
257 }
258
259 static ssize_t hmc5843_set_operating_mode(struct device *dev,
260                                 struct device_attribute *attr,
261                                 const char *buf,
262                                 size_t count)
263 {
264         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
265         struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
266         struct hmc5843_data *data = iio_priv(indio_dev);
267         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
268         unsigned long operating_mode = 0;
269         s32 status;
270         int error;
271
272         mutex_lock(&data->lock);
273         error = kstrtoul(buf, 10, &operating_mode);
274         if (error) {
275                 count = error;
276                 goto exit;
277         }
278         dev_dbg(dev, "set conversion mode to %lu\n", operating_mode);
279         if (operating_mode > HMC5843_MODE_SLEEP) {
280                 count = -EINVAL;
281                 goto exit;
282         }
283
284         status = i2c_smbus_write_byte_data(client, this_attr->address,
285                                         operating_mode);
286         if (status) {
287                 count = -EINVAL;
288                 goto exit;
289         }
290         data->operating_mode = operating_mode;
291
292 exit:
293         mutex_unlock(&data->lock);
294         return count;
295 }
296
297 static IIO_DEVICE_ATTR(operating_mode,
298                         S_IWUSR | S_IRUGO,
299                         hmc5843_show_operating_mode,
300                         hmc5843_set_operating_mode,
301                         HMC5843_MODE_REG);
302
303 /*
304  * API for setting the measurement configuration to
305  * Normal, Positive bias and Negative bias
306  *
307  * From the datasheet:
308  * 0 - Normal measurement configuration (default): In normal measurement
309  *     configuration the device follows normal measurement flow. Pins BP
310  *     and BN are left floating and high impedance.
311  *
312  * 1 - Positive bias configuration: In positive bias configuration, a
313  *     positive current is forced across the resistive load on pins BP
314  *     and BN.
315  *
316  * 2 - Negative bias configuration. In negative bias configuration, a
317  *     negative current is forced across the resistive load on pins BP
318  *     and BN.
319  *
320  */
321 static s32 hmc5843_set_meas_conf(struct i2c_client *client,
322                                       u8 meas_conf)
323 {
324         struct iio_dev *indio_dev = i2c_get_clientdata(client);
325         struct hmc5843_data *data = iio_priv(indio_dev);
326         u8 reg_val;
327         reg_val = (meas_conf & HMC5843_MEAS_CONF_MASK) |
328                 (data->rate << HMC5843_RATE_OFFSET);
329         return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
330 }
331
332 static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
333                                                 struct device_attribute *attr,
334                                                 char *buf)
335 {
336         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
337         struct hmc5843_data *data = iio_priv(indio_dev);
338         return sprintf(buf, "%d\n", data->meas_conf);
339 }
340
341 static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
342                                                 struct device_attribute *attr,
343                                                 const char *buf,
344                                                 size_t count)
345 {
346         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
347         struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
348         struct hmc5843_data *data = iio_priv(indio_dev);
349         unsigned long meas_conf = 0;
350         int error;
351
352         error = kstrtoul(buf, 10, &meas_conf);
353         if (error)
354                 return error;
355         if (meas_conf >= HMC5843_MEAS_CONF_NOT_USED)
356                 return -EINVAL;
357
358         mutex_lock(&data->lock);
359         dev_dbg(dev, "set measurement configuration to %lu\n", meas_conf);
360         if (hmc5843_set_meas_conf(client, meas_conf)) {
361                 count = -EINVAL;
362                 goto exit;
363         }
364         data->meas_conf = meas_conf;
365
366 exit:
367         mutex_unlock(&data->lock);
368         return count;
369 }
370
371 static IIO_DEVICE_ATTR(meas_conf,
372                         S_IWUSR | S_IRUGO,
373                         hmc5843_show_measurement_configuration,
374                         hmc5843_set_measurement_configuration,
375                         0);
376
377 static ssize_t hmc5843_show_sampling_frequencies_available(struct device *dev,
378                                                 struct device_attribute *attr,
379                                                 char *buf)
380 {
381         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
382         struct hmc5843_data *data = iio_priv(indio_dev);
383         ssize_t total_n = 0;
384         int i;
385
386         for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
387                 ssize_t n = sprintf(buf, "%s ", data->variant->regval_to_sample_freq[i]);
388                 buf += n;
389                 total_n += n;
390         }
391         /* replace trailing space by newline */
392         buf[-1] = '\n';
393
394         return total_n;
395 }
396
397 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_sampling_frequencies_available);
398
399 static s32 hmc5843_set_rate(struct i2c_client *client,
400                                 u8 rate)
401 {
402         struct iio_dev *indio_dev = i2c_get_clientdata(client);
403         struct hmc5843_data *data = iio_priv(indio_dev);
404         u8 reg_val;
405
406         if (rate >= HMC5843_RATE_NOT_USED) {
407                 dev_err(&client->dev,
408                         "data output rate is not supported\n");
409                 return -EINVAL;
410         }
411
412         reg_val = data->meas_conf | (rate << HMC5843_RATE_OFFSET);
413         return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
414 }
415
416 static int hmc5843_check_sampling_frequency(struct hmc5843_data *data,
417                                                 const char *buf)
418 {
419         const char * const *samp_freq = data->variant->regval_to_sample_freq;
420         int i;
421
422         for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
423                 if (sysfs_streq(buf, samp_freq[i]))
424                         return i;
425         }
426
427         return -EINVAL;
428 }
429
430 static ssize_t hmc5843_set_sampling_frequency(struct device *dev,
431                                         struct device_attribute *attr,
432                                         const char *buf, size_t count)
433 {
434
435         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
436         struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
437         struct hmc5843_data *data = iio_priv(indio_dev);
438         int rate;
439
440         rate = hmc5843_check_sampling_frequency(data, buf);
441         if (rate < 0) {
442                 dev_err(&client->dev,
443                         "sampling frequency is not supported\n");
444                 return rate;
445         }
446
447         mutex_lock(&data->lock);
448         dev_dbg(dev, "set rate to %d\n", rate);
449         if (hmc5843_set_rate(client, rate)) {
450                 count = -EINVAL;
451                 goto exit;
452         }
453         data->rate = rate;
454
455 exit:
456         mutex_unlock(&data->lock);
457         return count;
458 }
459
460 static ssize_t hmc5843_show_sampling_frequency(struct device *dev,
461                         struct device_attribute *attr, char *buf)
462 {
463         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
464         struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
465         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
466         struct hmc5843_data *data = iio_priv(indio_dev);
467         s32 rate;
468
469         rate = i2c_smbus_read_byte_data(client, this_attr->address);
470         if (rate < 0)
471                 return rate;
472         rate = (rate & HMC5843_RATE_BITMASK) >> HMC5843_RATE_OFFSET;
473         return sprintf(buf, "%s\n", data->variant->regval_to_sample_freq[rate]);
474 }
475
476 static IIO_DEVICE_ATTR(sampling_frequency,
477                         S_IWUSR | S_IRUGO,
478                         hmc5843_show_sampling_frequency,
479                         hmc5843_set_sampling_frequency,
480                         HMC5843_CONFIG_REG_A);
481
482 static ssize_t hmc5843_show_range_gain(struct device *dev,
483                                 struct device_attribute *attr,
484                                 char *buf)
485 {
486         u8 range;
487         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
488         struct hmc5843_data *data = iio_priv(indio_dev);
489
490         range = data->range;
491         return sprintf(buf, "%d\n", data->variant->regval_to_input_field_mga[range]);
492 }
493
494 static ssize_t hmc5843_set_range_gain(struct device *dev,
495                         struct device_attribute *attr,
496                         const char *buf,
497                         size_t count)
498 {
499         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
500         struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
501         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
502         struct hmc5843_data *data = iio_priv(indio_dev);
503         unsigned long range = 0;
504         int error;
505
506         mutex_lock(&data->lock);
507         error = kstrtoul(buf, 10, &range);
508         if (error) {
509                 count = error;
510                 goto exit;
511         }
512         dev_dbg(dev, "set range to %lu\n", range);
513
514         if (range > HMC5843_RANGE_GAIN_MAX) {
515                 count = -EINVAL;
516                 goto exit;
517         }
518
519         data->range = range;
520         range = range << HMC5843_RANGE_GAIN_OFFSET;
521         if (i2c_smbus_write_byte_data(client, this_attr->address, range))
522                 count = -EINVAL;
523
524 exit:
525         mutex_unlock(&data->lock);
526         return count;
527 }
528
529 static IIO_DEVICE_ATTR(in_magn_range,
530                         S_IWUSR | S_IRUGO,
531                         hmc5843_show_range_gain,
532                         hmc5843_set_range_gain,
533                         HMC5843_CONFIG_REG_B);
534
535 static int hmc5843_read_raw(struct iio_dev *indio_dev,
536                             struct iio_chan_spec const *chan,
537                             int *val, int *val2,
538                             long mask)
539 {
540         struct hmc5843_data *data = iio_priv(indio_dev);
541
542         switch (mask) {
543         case IIO_CHAN_INFO_RAW:
544                 return hmc5843_read_measurement(indio_dev,
545                                                 chan->address,
546                                                 val);
547         case IIO_CHAN_INFO_SCALE:
548                 *val = 0;
549                 *val2 = data->variant->regval_to_nanoscale[data->range];
550                 return IIO_VAL_INT_PLUS_NANO;
551         }
552         return -EINVAL;
553 }
554
555 #define HMC5843_CHANNEL(axis, addr)                                     \
556         {                                                               \
557                 .type = IIO_MAGN,                                       \
558                 .modified = 1,                                          \
559                 .channel2 = IIO_MOD_##axis,                             \
560                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
561                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
562                 .address = addr                                         \
563         }
564
565 static const struct iio_chan_spec hmc5843_channels[] = {
566         HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
567         HMC5843_CHANNEL(Y, HMC5843_DATA_OUT_Y_MSB_REG),
568         HMC5843_CHANNEL(Z, HMC5843_DATA_OUT_Z_MSB_REG),
569 };
570
571 static const struct iio_chan_spec hmc5883_channels[] = {
572         HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
573         HMC5843_CHANNEL(Y, HMC5883_DATA_OUT_Y_MSB_REG),
574         HMC5843_CHANNEL(Z, HMC5883_DATA_OUT_Z_MSB_REG),
575 };
576
577 static struct attribute *hmc5843_attributes[] = {
578         &iio_dev_attr_meas_conf.dev_attr.attr,
579         &iio_dev_attr_operating_mode.dev_attr.attr,
580         &iio_dev_attr_sampling_frequency.dev_attr.attr,
581         &iio_dev_attr_in_magn_range.dev_attr.attr,
582         &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
583         NULL
584 };
585
586 static const struct attribute_group hmc5843_group = {
587         .attrs = hmc5843_attributes,
588 };
589
590 static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
591         [HMC5843_ID] = {
592                 .channels = hmc5843_channels,
593                 .regval_to_sample_freq = hmc5843_regval_to_sample_freq,
594                 .regval_to_input_field_mga =
595                         hmc5843_regval_to_input_field_mga,
596                 .regval_to_nanoscale = hmc5843_regval_to_nanoscale,
597         },
598         [HMC5883_ID] = {
599                 .channels = hmc5883_channels,
600                 .regval_to_sample_freq = hmc5883_regval_to_sample_freq,
601                 .regval_to_input_field_mga =
602                         hmc5883_regval_to_input_field_mga,
603                 .regval_to_nanoscale = hmc5883_regval_to_nanoscale,
604         },
605         [HMC5883L_ID] = {
606                 .channels = hmc5883_channels,
607                 .regval_to_sample_freq = hmc5883_regval_to_sample_freq,
608                 .regval_to_input_field_mga =
609                         hmc5883l_regval_to_input_field_mga,
610                 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
611         },
612 };
613
614 /* Called when we have found a new HMC58X3 */
615 static void hmc5843_init_client(struct i2c_client *client,
616                                 const struct i2c_device_id *id)
617 {
618         struct iio_dev *indio_dev = i2c_get_clientdata(client);
619         struct hmc5843_data *data = iio_priv(indio_dev);
620
621         data->variant = &hmc5843_chip_info_tbl[id->driver_data];
622         indio_dev->channels = data->variant->channels;
623         indio_dev->num_channels = 3;
624         hmc5843_set_meas_conf(client, data->meas_conf);
625         hmc5843_set_rate(client, data->rate);
626         hmc5843_configure(client, data->operating_mode);
627         i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_B, data->range);
628         mutex_init(&data->lock);
629
630         pr_info("%s initialized\n", id->name);
631 }
632
633 static const struct iio_info hmc5843_info = {
634         .attrs = &hmc5843_group,
635         .read_raw = &hmc5843_read_raw,
636         .driver_module = THIS_MODULE,
637 };
638
639 static int hmc5843_probe(struct i2c_client *client,
640                          const struct i2c_device_id *id)
641 {
642         struct hmc5843_data *data;
643         struct iio_dev *indio_dev;
644         int err = 0;
645
646         indio_dev = iio_device_alloc(sizeof(*data));
647         if (indio_dev == NULL) {
648                 err = -ENOMEM;
649                 goto exit;
650         }
651
652         /* default settings at probe */
653         data = iio_priv(indio_dev);
654         data->meas_conf = HMC5843_MEAS_CONF_NORMAL;
655         data->range = HMC5843_RANGE_GAIN_DEFAULT;
656         data->operating_mode = HMC5843_MODE_CONVERSION_CONTINUOUS;
657
658         i2c_set_clientdata(client, indio_dev);
659         hmc5843_init_client(client, id);
660
661         indio_dev->info = &hmc5843_info;
662         indio_dev->name = id->name;
663         indio_dev->dev.parent = &client->dev;
664         indio_dev->modes = INDIO_DIRECT_MODE;
665
666         err = iio_device_register(indio_dev);
667         if (err)
668                 goto exit_free2;
669
670         return 0;
671
672 exit_free2:
673         iio_device_free(indio_dev);
674 exit:
675         return err;
676 }
677
678 static int hmc5843_remove(struct i2c_client *client)
679 {
680         struct iio_dev *indio_dev = i2c_get_clientdata(client);
681
682         iio_device_unregister(indio_dev);
683          /*  sleep mode to save power */
684         hmc5843_configure(client, HMC5843_MODE_SLEEP);
685         iio_device_free(indio_dev);
686
687         return 0;
688 }
689
690 #ifdef CONFIG_PM_SLEEP
691 static int hmc5843_suspend(struct device *dev)
692 {
693         hmc5843_configure(to_i2c_client(dev), HMC5843_MODE_SLEEP);
694         return 0;
695 }
696
697 static int hmc5843_resume(struct device *dev)
698 {
699         struct i2c_client *client = to_i2c_client(dev);
700         struct iio_dev *indio_dev = i2c_get_clientdata(client);
701         struct hmc5843_data *data = iio_priv(indio_dev);
702
703         hmc5843_configure(client, data->operating_mode);
704
705         return 0;
706 }
707
708 static SIMPLE_DEV_PM_OPS(hmc5843_pm_ops, hmc5843_suspend, hmc5843_resume);
709 #define HMC5843_PM_OPS (&hmc5843_pm_ops)
710 #else
711 #define HMC5843_PM_OPS NULL
712 #endif
713
714 static const struct i2c_device_id hmc5843_id[] = {
715         { "hmc5843", HMC5843_ID },
716         { "hmc5883", HMC5883_ID },
717         { "hmc5883l", HMC5883L_ID },
718         { }
719 };
720 MODULE_DEVICE_TABLE(i2c, hmc5843_id);
721
722 static struct i2c_driver hmc5843_driver = {
723         .driver = {
724                 .name   = "hmc5843",
725                 .pm     = HMC5843_PM_OPS,
726         },
727         .id_table       = hmc5843_id,
728         .probe          = hmc5843_probe,
729         .remove         = hmc5843_remove,
730 };
731 module_i2c_driver(hmc5843_driver);
732
733 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com");
734 MODULE_DESCRIPTION("HMC5843/5883/5883L driver");
735 MODULE_LICENSE("GPL");