summaryrefslogtreecommitdiff
path: root/drivers/staging/iio/magnetometer/lsm303dlh.c
blob: 076b60ba1635e8db56c10784058cd628a4c41f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
/*
 * Copyright (C) ST-Ericsson SA 2012
 * License Terms: GNU General Public License, version 2
 *
 * This code is mostly based on hmc5843 driver
 *
 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/regulator/consumer.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#include <linux/lsm303dlh.h>

#include "../iio.h"
#include "../sysfs.h"

/* configuration register A */
#define LSM303DLH_M_CRA_REG	0x00
/* configuration register B */
#define LSM303DLH_M_CRB_REG	0x01
/* mode register */
#define LSM303DLH_M_MR_REG	0x02
/* data output X register */
#define LSM303DLH_M_OUT_X	0x03
/* data output Y register */
#define LSM303DLH_M_OUT_Y	0x05
/* data output Z register */
#define LSM303DLH_M_OUT_Z	0x07
/* status register */
#define LSM303DLH_M_SR_REG	0x09
/* identification registers */
#define LSM303DLH_M_IRA_REG	0x0A
#define LSM303DLH_M_IRB_REG	0x0B
#define LSM303DLH_M_IRC_REG	0x0C

/* control register A, Data Output rate */
#define LSM303DLH_M_CRA_DO_BIT	2
#define LSM303DLH_M_CRA_DO_MASK	(0x7 << LSM303DLH_M_CRA_DO_BIT)
/* control register A, measurement configuration */
#define LSM303DLH_M_CRA_MS_BIT	0
#define LSM303DLH_M_CRA_MS_MASK	(0x3 << LSM303DLH_M_CRA_MS_BIT)
/* control register B, gain configuration */
#define LSM303DLH_M_CRB_GN_BIT	5
#define LSM303DLH_M_CRB_GN_MASK	(0x7 << LSM303DLH_M_CRB_GN_BIT)
/* mode register */
#define LSM303DLH_M_MR_MD_BIT	0
#define LSM303DLH_M_MR_MD_MASK	(0x3 << LSM303DLH_M_MR_MD_BIT)
/* status register, ready  */
#define LSM303DLH_M_SR_RDY_BIT	0
#define LSM303DLH_M_SR_RDY_MASK	(0x1 << LSM303DLH_M_SR_RDY_BIT)
/* status register, data output register lock */
#define LSM303DLH_M_SR_LOC_BIT	1
#define LSM303DLH_M_SR_LOC_MASK	(0x1 << LSM303DLH_M_SR_LOC_BIT)
/* status register, regulator enabled */
#define LSM303DLH_M_SR_REN_BIT	2
#define LSM303DLH_M_SR_REN_MASK	(0x1 << LSM303DLH_M_SR_REN_BIT)

/*
 *     Control register gain settings
 *---------------------------------------------
 *GN2 | GN1| GN0|sensor input| Gain X/Y | Gain Z|
 * 0  |  0 |  1 |     +/-1.3 |   1055   |   950 |
 * 0  |  1 |  0 |     +/-1.9 |   795    |   710 |
 * 0  |  1 |  1 |     +/-2.5 |   635    |   570 |
 * 1  |  0 |  0 |     +/-4.0 |   430    |   385 |
 * 1  |  0 |  1 |     +/-4.7 |   375    |   335 |
 * 1  |  1 |  0 |     +/-5.6 |   320    |   285 |
 * 1  |  1 |  1 |     +/-8.1 |   230    |   205 |
 *---------------------------------------------
 */
#define LSM303DLH_M_RANGE_1_3G	0x01
#define LSM303DLH_M_RANGE_1_9G	0x02
#define LSM303DLH_M_RANGE_2_5G	0x03
#define LSM303DLH_M_RANGE_4_0G	0x04
#define LSM303DLH_M_RANGE_4_7G	0x05
#define LSM303DLH_M_RANGE_5_6G	0x06
#define LSM303DLH_M_RANGE_8_1G	0x07

/*
 * CRA register data output rate settings
 *
 * DO2 DO1 DO0 Minimum data output rate (Hz)
 * 0    0   0		0.75
 * 0    0   1		1.5
 * 0    1   0		3.0
 * 0    1   1		7.5
 * 1    0   0		15
 * 1    0   1		30
 * 1    1   0		75
 * 1    1   1		Not used
 */
#define LSM303DLH_M_RATE_00_75		0x00
#define LSM303DLH_M_RATE_01_50		0x01
#define LSM303DLH_M_RATE_03_00		0x02
#define LSM303DLH_M_RATE_07_50		0x03
#define LSM303DLH_M_RATE_15_00		0x04
#define LSM303DLH_M_RATE_30_00		0x05
#define LSM303DLH_M_RATE_75_00		0x06
#define LSM303DLH_M_RATE_RESERVED	0x07

/* device status defines */
#define LSM303DLH_M_DEVICE_OFF		0
#define LSM303DLH_M_DEVICE_ON		1
#define LSM303DLH_M_DEVICE_SUSPENDED	2

#define	LSM303DLH_M_NORMAL_CFG		0x00
#define	LSM303DLH_M_POSITIVE_BIAS_CFG	0x01
#define	LSM303DLH_M_NEGATIVE_BIAS_CFG	0x02
#define	LSM303DLH_M_NOT_USED_CFG	0x03

/* Magnetic sensor operating mode */
#define LSM303DLH_M_CONTINUOUS_CONVERSION_MODE	0x00
#define LSM303DLH_M_SINGLE_CONVERSION_MODE	0x01
#define LSM303DLH_M_UNUSED_MODE			0x02
#define LSM303DLH_M_SLEEP_MODE			0x03

/* Multiple byte transfer enable */
#define LSM303DLH_MULTIPLE_I2C_TR	0x80
#define LSM303DLH_M_DATA_RDY		0x01

/* device CHIP ID defines */
#define LSM303DLHC_CHIP_ID		51

/*
 * The scaling frequencies are different
 * for LSM303DLH and LSM303DLHC
 * the number of elments of scaling frequency
 * is 50 and hence set this as the array size
 */
#define XY_LENGTH		50
#define Z_LENGTH		50

char xy_scale_avail[XY_LENGTH];
char z_scale_avail[Z_LENGTH];

/*
 * struct lsm303dlh_m_data - data structure used by lsm303dlh_m driver
 * @client: i2c client
 * @lock: mutex lock for sysfs operations
 * @regulator: regulator
 * @early_suspend: early suspend structure
 * @pdata: lsm303dlh platform data pointer
 * @device_status: device is ON, OFF or SUSPENDED
 * @mode: current mode of operation
 * @rate: current sampling rate
 * @config: device configuration
 * @range: current range value of magnetometer
 */

struct lsm303dlh_m_data {
	struct i2c_client	*client;
	struct mutex		lock;
	struct regulator	*regulator;
#ifdef CONFIG_HAS_EARLYSUSPEND
	struct early_suspend early_suspend;
#endif
	struct lsm303dlh_platform_data *pdata;
	int			device_status;
	u8			mode;
	u8			rate;
	u8			config;
	u8			range;
};

#ifdef CONFIG_HAS_EARLYSUSPEND
static void lsm303dlh_m_early_suspend(struct early_suspend *data);
static void lsm303dlh_m_late_resume(struct early_suspend *data);
#endif

static s32 lsm303dlh_config(struct i2c_client *client, u8 mode)
{
	/* the lower two bits indicates the magnetic sensor mode */
	return i2c_smbus_write_byte_data(client,
					LSM303DLH_M_MR_REG, mode & 0x03);
}

static inline int is_device_on(struct lsm303dlh_m_data *data)
{
	struct i2c_client *client = data->client;
	/*  Perform read/write operation only when device is active */
	if (data->device_status != LSM303DLH_M_DEVICE_ON) {
		dev_err(&client->dev,
			"device is switched off, make it on using mode");
		return -EINVAL;
	}

	return 0;
}

/* disable regulator and update status */
static int lsm303dlh_m_disable(struct lsm303dlh_m_data *data)
{
	data->device_status = LSM303DLH_M_DEVICE_OFF;

	regulator_disable(data->regulator);

	return 0;
}

/* enable regulator and update status */
static int lsm303dlh_m_enable(struct lsm303dlh_m_data *data)
{
	data->device_status = LSM303DLH_M_DEVICE_ON;

	regulator_enable(data->regulator);

	return 0;
}

/*
 * To read output x/y/z data register,
 * in this case x,y and z are not
 * mapped w.r.t board orientation.
 * Reading just raw data from device
 */
static ssize_t lsm303dlh_m_xyz_read(struct iio_dev *indio_dev,
					int address,
					int *buf)
{
	struct lsm303dlh_m_data *data = iio_priv(indio_dev);
	int ret;

	/* Perform read/write operation, only when device is active */
	ret = is_device_on(data);
	if (ret)
		return -EINVAL;

	mutex_lock(&data->lock);

	ret = i2c_smbus_read_byte_data(data->client, LSM303DLH_M_SR_REG);

	/* wait till data is written to all six registers */
	while (!(ret & LSM303DLH_M_DATA_RDY))
		ret = i2c_smbus_read_byte_data(data->client,
						LSM303DLH_M_SR_REG);

	ret = i2c_smbus_read_word_swapped(data->client, address);

	if (ret < 0) {
		dev_err(&data->client->dev, "reading xyz failed\n");
		mutex_unlock(&data->lock);
		return -EINVAL;
	}

	mutex_unlock(&data->lock);

	*buf = (s16)ret;

	return IIO_VAL_INT;
}

/*
 * To read output x,y,z data register.
 * After reading change x,y and z values
 * w.r.t the orientation of the device.
 */
static ssize_t lsm303dlh_m_readdata(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev));
	struct lsm303dlh_platform_data *pdata = data->pdata;
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
	u8 map_x = pdata->axis_map_x;
	u8 map_y = pdata->axis_map_y;
	u8 map_z = pdata->axis_map_z;
	int ret;
	unsigned char magn_data[6];
	s16 val[3];

	/* Perform read/write operation, only when device is active */
	ret = is_device_on(data);
	if (ret)
		return -EINVAL;

	mutex_lock(&data->lock);

	ret = i2c_smbus_read_byte_data(data->client, LSM303DLH_M_SR_REG);

	/* wait till data is written to all six registers */
	while (!(ret & LSM303DLH_M_DATA_RDY))
		ret = i2c_smbus_read_byte_data(data->client,
					LSM303DLH_M_SR_REG);

	ret = i2c_smbus_read_i2c_block_data(data->client,
				this_attr->address |
				LSM303DLH_MULTIPLE_I2C_TR,
				6, magn_data);

	if (ret < 0) {
		dev_err(&data->client->dev, "reading xyz failed\n");
		mutex_unlock(&data->lock);
		return -EINVAL;
	}

	/* MSB is at lower address */
	val[0] = (s16)
			(((magn_data[0]) << 8) | magn_data[1]);
	val[1] = (s16)
			(((magn_data[2]) << 8) | magn_data[3]);
	val[2] = (s16)
			(((magn_data[4]) << 8) | magn_data[5]);
	/* check if chip is DHLC */
	if (data->pdata->chip_id == LSM303DLHC_CHIP_ID)
		/*
		 * the out registers are in x, z and y order
		 * so swap y and z values
		 */
		swap(val[1], val[2]);
	/* modify the x,y and z values w.r.t orientation of device*/
	if (pdata->negative_x)
		val[map_x] = -val[map_x];
	if (pdata->negative_y)
		val[map_y] = -val[map_y];
	if (pdata->negative_z)
		val[map_z] = -val[map_z];
	mutex_unlock(&data->lock);

	return sprintf(buf, "%d:%d:%d:%lld\n", val[map_x], val[map_y],
				val[map_z], iio_get_time_ns());
}

static ssize_t show_operating_mode(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev));

	return sprintf(buf, "%d\n", data->mode);
}

static ssize_t set_operating_mode(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev));
	struct i2c_client *client = data->client;
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
	int error;
	unsigned long mode = 0;

	mutex_lock(&data->lock);

	error = kstrtoul(buf, 10, &mode);
	if (error) {
		count = error;
		goto exit;
	}

	if (mode > LSM303DLH_M_SLEEP_MODE) {
		dev_err(&client->dev, "trying to set invalid mode\n");
		count = -EINVAL;
		goto exit;
	}

	/*
	 * If device is driven to sleep mode in suspend, update mode
	 * and return
	 */
	if (data->device_status == LSM303DLH_M_DEVICE_SUSPENDED &&
			mode == LSM303DLH_M_SLEEP_MODE) {
		data->mode = mode;
		goto exit;
	}

	if (data->mode == mode)
		goto exit;

	/* Enable the regulator if it is not turned on earlier */
	if (data->device_status == LSM303DLH_M_DEVICE_OFF ||
		data->device_status == LSM303DLH_M_DEVICE_SUSPENDED)
		lsm303dlh_m_enable(data);

	dev_dbg(dev, "set operating mode to %lu\n", mode);

	error = i2c_smbus_write_byte_data(client, this_attr->address, mode);
	if (error < 0) {
		dev_err(&client->dev, "Error in setting the mode\n");
		count = -EINVAL;
		goto exit;
	}

	data->mode = mode;
	/* if sleep mode, disable the regulator */
	if (data->mode == LSM303DLH_M_SLEEP_MODE)
		lsm303dlh_m_disable(data);
exit:
	mutex_unlock(&data->lock);
	return count;
}

/*
 * Magnetic sensor operating mode: CRA_REG
 * ms1 ms0
 * 0	0	Normal measurement configuration
 * 0	1	Positive bias configuration.
 * 1	0	Negative bias configuration.
 * 1	1	This configuration is not used
 */
static s32 lsm303dlh_set_config(struct i2c_client *client, u8 config)
{
	struct lsm303dlh_m_data *data = i2c_get_clientdata(client);
	u8 reg_val;

	reg_val = (config & LSM303DLH_M_CRA_MS_MASK) |
			(data->rate << LSM303DLH_M_CRA_DO_BIT);
	return i2c_smbus_write_byte_data(client, LSM303DLH_M_CRA_REG, reg_val);
}


static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("0.75 1.5 3.0 7.5 15 30 75");

static s32 lsm303dlh_m_set_range(struct i2c_client *client, u8 range)
{
	u8 reg_val;

	reg_val = range << LSM303DLH_M_CRB_GN_BIT;

	return i2c_smbus_write_byte_data(client, LSM303DLH_M_CRB_REG, reg_val);
}

static s32 lsm303dlh_m_set_rate(struct i2c_client *client, u8 rate)
{
	struct lsm303dlh_m_data *data = i2c_get_clientdata(client);
	u8 reg_val;

	reg_val =  (data->config) | (rate << LSM303DLH_M_CRA_DO_BIT);
	if (rate >= LSM303DLH_M_RATE_RESERVED) {
		dev_err(&client->dev, "given rate not supported\n");
		return -EINVAL;
	}

	return i2c_smbus_write_byte_data(client, LSM303DLH_M_CRA_REG, reg_val);
}

static ssize_t set_sampling_frequency(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev));
	struct i2c_client *client = data->client;
	unsigned long rate = 0;
	int err;

	err = is_device_on(data);
	if (err)
		return err;

	if (strncmp(buf, "0.75" , 4) == 0)
		rate = LSM303DLH_M_RATE_00_75;

	else if (strncmp(buf, "1.5" , 3) == 0)
		rate = LSM303DLH_M_RATE_01_50;

	else if (strncmp(buf, "3.0" , 3) == 0)
		rate = LSM303DLH_M_RATE_03_00;

	else if (strncmp(buf, "7.5" , 3) == 0)
		rate = LSM303DLH_M_RATE_07_50;

	else if (strncmp(buf, "15" , 2) == 0)
		rate = LSM303DLH_M_RATE_15_00;

	else if (strncmp(buf, "30" , 2) == 0)
		rate = LSM303DLH_M_RATE_30_00;

	else if (strncmp(buf, "75" , 2) == 0)
		rate = LSM303DLH_M_RATE_75_00;
	else
		return -EINVAL;

	mutex_lock(&data->lock);

	if (lsm303dlh_m_set_rate(client, rate)) {
		dev_err(&client->dev, "set rate failed\n");
		count = -EINVAL;
		goto exit;
	}
	data->rate = rate;

exit:
	mutex_unlock(&data->lock);
	return count;
}

/* sampling frequency - output rate in Hz */
static const char * const reg_to_rate[] = {
	"0.75",
	"1.5",
	"3.0",
	"7.5",
	"15",
	"30",
	"75",
	"res",
};

static int xy_to_nanoscale[] = {
	947870, 1257860, 1574800, 2325580, 2666670, 3125000, 4347830
};

static int z_to_nanoscale[] = {
	1052630, 1408450, 17543820, 2597400, 2985070, 3508770, 4878050
};

static int xy_to_nanoscale_dlhc[] = {
	 909090, 1169590, 1492540, 2222220, 2500000, 3030300, 4347830
};

static int z_to_nanoscale_dlhc[] = {
	1020410, 1315790, 1666660, 2500000, 2816900, 3389830, 4878050
};

static const char const scale_to_range[] = {
	LSM303DLH_M_RANGE_1_3G,
	LSM303DLH_M_RANGE_1_9G,
	LSM303DLH_M_RANGE_2_5G,
	LSM303DLH_M_RANGE_4_0G,
	LSM303DLH_M_RANGE_4_7G,
	LSM303DLH_M_RANGE_5_6G,
	LSM303DLH_M_RANGE_8_1G
};
static ssize_t show_sampling_frequency(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev));

	return sprintf(buf, "%s\n", reg_to_rate[data->rate]);
}

static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
			show_sampling_frequency,
			set_sampling_frequency);

static IIO_CONST_ATTR(magnet_xy_scale_available, xy_scale_avail);

static IIO_CONST_ATTR(magnet_z_scale_available, z_scale_avail);

static int lsm303dlh_write_raw(struct iio_dev *indio_dev,
			struct iio_chan_spec const *chan,
			int val, int val2,
			long mask)
{
	struct lsm303dlh_m_data *data = iio_priv(indio_dev);
	int ret = -EINVAL, i;
	bool flag = false;
	char end;
	int *xy_scale;
	int *z_scale;

	switch (mask) {
	case IIO_CHAN_INFO_SCALE:
		ret = is_device_on(data);
		if (ret)
			return -EINVAL;
		mutex_lock(&data->lock);

		if (data->pdata->chip_id == LSM303DLHC_CHIP_ID) {
			xy_scale = xy_to_nanoscale_dlhc;
			z_scale = z_to_nanoscale_dlhc;
		} else {
			xy_scale = xy_to_nanoscale;
			z_scale = z_to_nanoscale;
		}
		end = ARRAY_SIZE(xy_to_nanoscale);
		if (chan->address == LSM303DLH_M_OUT_X ||
				chan->address == LSM303DLH_M_OUT_Y) {
			for (i = 0; i < end; i++) {
				if (val == xy_scale[i]) {
					flag = true;
					break;
				}
			}
		} else if (chan->address == LSM303DLH_M_OUT_Z) {
			for (i = 0; i < end; i++) {
				if (val == z_scale[i]) {
					flag = true;
					break;
				}
			}
		}
		if (flag) {
			ret = lsm303dlh_m_set_range(data->client,
						scale_to_range[data->range]);
			data->range = i;
		}
		mutex_unlock(&data->lock);
		break;
	default:
		break;
	}
	return ret;
}

static int lsm303dlh_read_raw(struct iio_dev *indio_dev,
			struct iio_chan_spec const *chan,
			int *val, int *val2,
			long mask)
{
	struct lsm303dlh_m_data *data = iio_priv(indio_dev);

	switch (mask) {
	case 0:
		return lsm303dlh_m_xyz_read(indio_dev,
					chan->address, val);
	case IIO_CHAN_INFO_SCALE:
		/* scale for X/Y and Z are different */
		if (chan->address == LSM303DLH_M_OUT_X ||
				chan->address == LSM303DLH_M_OUT_Y){
			/* check if chip is DHLC */
			if (data->pdata->chip_id == LSM303DLHC_CHIP_ID)
				*val2 = xy_to_nanoscale_dlhc[data->range];
			else
				*val2 = xy_to_nanoscale[data->range];
		} else {
			/* check if chip is DHLC */
			if (data->pdata->chip_id == LSM303DLHC_CHIP_ID)
				*val2 = z_to_nanoscale_dlhc[data->range];
			else
				*val2 = z_to_nanoscale[data->range];
		}

		return IIO_VAL_INT_PLUS_NANO;
	default:
		break;
	}
	return -EINVAL;
}

#define LSM303DLH_CHANNEL(axis, addr)				\
	{							\
		.type = IIO_MAGN,				\
		.modified = 1,					\
		.channel2 = IIO_MOD_##axis,			\
		.info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,	\
		.address = addr,				\
	}

static const struct iio_chan_spec lsmdlh303_channels[] = {
	LSM303DLH_CHANNEL(X, LSM303DLH_M_OUT_X),
	LSM303DLH_CHANNEL(Y, LSM303DLH_M_OUT_Y),
	LSM303DLH_CHANNEL(Z, LSM303DLH_M_OUT_Z),
};

static IIO_DEVICE_ATTR(mode,
			S_IWUSR | S_IRUGO,
			show_operating_mode,
			set_operating_mode,
			LSM303DLH_M_MR_REG);
static IIO_DEVICE_ATTR(magn_raw, S_IRUGO,
			lsm303dlh_m_readdata,
			NULL,
			LSM303DLH_M_OUT_X);

static struct attribute *lsm303dlh_m_attributes[] = {
	&iio_dev_attr_mode.dev_attr.attr,
	&iio_dev_attr_sampling_frequency.dev_attr.attr,
	&iio_dev_attr_magn_raw.dev_attr.attr,
	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
	&iio_const_attr_magnet_xy_scale_available.dev_attr.attr,
	&iio_const_attr_magnet_z_scale_available.dev_attr.attr,
	NULL
};

static const struct attribute_group lsmdlh303m_group = {
	.attrs = lsm303dlh_m_attributes,
};

static const struct iio_info lsmdlh303m_info = {
	.attrs = &lsmdlh303m_group,
	.read_raw = &lsm303dlh_read_raw,
	.write_raw = &lsm303dlh_write_raw,
	.driver_module = THIS_MODULE,
};

static void lsm303dlh_m_setup(struct lsm303dlh_m_data *data)
{
	/* set the magnetic sensor operating mode */
	lsm303dlh_set_config(data->client, data->config);
	/* set to the default rate */
	lsm303dlh_m_set_rate(data->client, data->rate);
	/* set the magnetic sensor mode */
	lsm303dlh_config(data->client, data->mode);
	/* set the range */
	lsm303dlh_m_set_range(data->client, scale_to_range[data->range]);
}

#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM))
static int lsm303dlh_m_suspend(struct device *dev)
{
	struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev));
	int ret = 0;

	if (data->mode == LSM303DLH_M_SLEEP_MODE)
		return 0;

	mutex_lock(&data->lock);

	/* Set the device to sleep mode */
	lsm303dlh_config(data->client, LSM303DLH_M_SLEEP_MODE);

	/* Disable regulator */
	lsm303dlh_m_disable(data);

	data->device_status = LSM303DLH_M_DEVICE_SUSPENDED;

	mutex_unlock(&data->lock);

	return ret;
}

static int lsm303dlh_m_resume(struct device *dev)
{
	struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev));
	int ret = 0;

	if (data->device_status == LSM303DLH_M_DEVICE_ON ||
		data->device_status == LSM303DLH_M_DEVICE_OFF) {
		return 0;
	}
	mutex_lock(&data->lock);

	/* Enable regulator */
	lsm303dlh_m_enable(data);

	/* Setup device parameters */
	lsm303dlh_m_setup(data);

	mutex_unlock(&data->lock);
	return ret;
}

static const struct dev_pm_ops lsm303dlh_m_dev_pm_ops = {
	.suspend = lsm303dlh_m_suspend,
	.resume = lsm303dlh_m_resume,
};
#endif
#ifdef CONFIG_HAS_EARLYSUSPEND
static void lsm303dlh_m_early_suspend(struct early_suspend *data)
{
	struct lsm303dlh_m_data *ddata =
		container_of(data, struct lsm303dlh_m_data, early_suspend);

	if (ddata->mode == LSM303DLH_M_SLEEP_MODE)
		return;

	mutex_lock(&ddata->lock);

	/* Set the device to sleep mode */
	lsm303dlh_config(ddata->client, LSM303DLH_M_SLEEP_MODE);

	/* Disable regulator */
	lsm303dlh_m_disable(ddata);

	ddata->device_status = LSM303DLH_M_DEVICE_SUSPENDED;

	mutex_unlock(&ddata->lock);
}

static void lsm303dlh_m_late_resume(struct early_suspend *data)
{
	struct lsm303dlh_m_data *ddata =
		container_of(data, struct lsm303dlh_m_data, early_suspend);

	if (ddata->device_status == LSM303DLH_M_DEVICE_ON ||
		ddata->device_status == LSM303DLH_M_DEVICE_OFF) {
		return;
	}
	mutex_lock(&ddata->lock);

	/* Enable regulator */
	lsm303dlh_m_enable(ddata);

	/* Setup device parameters */
	lsm303dlh_m_setup(ddata);

	mutex_unlock(&ddata->lock);

}
#endif

static int lsm303dlh_m_probe(struct i2c_client *client,
				const struct i2c_device_id *id)
{
	struct lsm303dlh_m_data *data;
	struct iio_dev *indio_dev;
	int err;

	indio_dev = iio_allocate_device(sizeof(*data));
	if (indio_dev == NULL) {
		dev_err(&client->dev, "memory allocation failed\n");
		err = -ENOMEM;
		goto exit;
	}

	data = iio_priv(indio_dev);

	data->mode = LSM303DLH_M_SLEEP_MODE;
	data->config = LSM303DLH_M_NORMAL_CFG;
	data->range = LSM303DLH_M_RANGE_1_3G;
	data->rate = LSM303DLH_M_RATE_00_75;

	data->client = client;

	/* check for valid platform data */
	if (!client->dev.platform_data) {
		dev_err(&client->dev, "Invalid platform data\n");
		err = -ENOMEM;
		goto exit1;
	}

	data->pdata = client->dev.platform_data;

	i2c_set_clientdata(client, indio_dev);

	data->regulator = regulator_get(&client->dev, "vdd");
	if (IS_ERR(data->regulator)) {
		dev_err(&client->dev, "failed to get regulator\n");
		err = PTR_ERR(data->regulator);
		goto exit1;
	}

	/* enable regulators */
	lsm303dlh_m_enable(data);

	lsm303dlh_m_setup(data);

	mutex_init(&data->lock);

	indio_dev->info = &lsmdlh303m_info;
	indio_dev->name = id->name;
	indio_dev->dev.parent = &client->dev;
	indio_dev->channels = lsmdlh303_channels;
	indio_dev->num_channels = ARRAY_SIZE(lsmdlh303_channels);
	indio_dev->modes = INDIO_DIRECT_MODE;

	err = iio_device_register(indio_dev);
	if (err)
		goto exit2;

#ifdef CONFIG_HAS_EARLYSUSPEND
	data->early_suspend.level =
				EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
	data->early_suspend.suspend = lsm303dlh_m_early_suspend;
	data->early_suspend.resume = lsm303dlh_m_late_resume;
	register_early_suspend(&data->early_suspend);
#endif

	/* disable regulator */
	lsm303dlh_m_disable(data);

	if (data->pdata->chip_id == LSM303DLHC_CHIP_ID)	{
		strcpy(xy_scale_avail, "909090, 1169590, 1492540, 2222220, 2500000, 3030300, 4347830");
		strcpy(z_scale_avail, "1020410, 1315790, 1666660, 2500000, 2816900, 3389830, 4878050");
	} else {
		strcpy(xy_scale_avail, "947870, 1257860, 1574800, 2325580, 2666670, 3125000, 4347830");
		strcpy(z_scale_avail, "947870, 1257860, 1574800, 2325580, 2666670, 3125000, 4347830");
	}

	return 0;

exit2:
	regulator_disable(data->regulator);
	mutex_destroy(&data->lock);
	regulator_put(data->regulator);
exit1:
	iio_free_device(indio_dev);
exit:
	return err;
}

static int __devexit lsm303dlh_m_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct lsm303dlh_m_data *data = iio_priv(indio_dev);
	int ret;

	/* its safe to set the mode to sleep */
	if (data->mode != LSM303DLH_M_SLEEP_MODE) {
		ret = lsm303dlh_config(client, LSM303DLH_M_SLEEP_MODE);
		if (ret < 0) {
			dev_err(&client->dev,
				"could not place the device in sleep mode %d",
				ret);
			return ret;
		}
		if (data->device_status == LSM303DLH_M_DEVICE_ON)
			regulator_disable(data->regulator);
		data->device_status = LSM303DLH_M_DEVICE_OFF;
	}
	regulator_put(data->regulator);
	mutex_destroy(&data->lock);
	iio_device_unregister(indio_dev);
	iio_free_device(indio_dev);

	return 0;
}

static const struct i2c_device_id lsm303dlh_m_id[] = {
	{ "lsm303dlh_m", 0 },
	{ },
};
MODULE_DEVICE_TABLE(i2c, lsm303dlh_m_id);

static struct i2c_driver lsm303dlh_m_driver = {
	.driver = {
		.name	= "lsm303dlh_m",
	#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM))
		.pm	= &lsm303dlh_m_dev_pm_ops,
	#endif
	},
	.id_table	= lsm303dlh_m_id,
	.probe		= lsm303dlh_m_probe,
	.remove		= lsm303dlh_m_remove,
};

module_i2c_driver(lsm303dlh_m_driver);

MODULE_DESCRIPTION("lsm303dlh Magnetometer Driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("srinidhi kasagar <srinidhi.kasagar@stericsson.com>");