1/*  Copyright (C) 2010 Texas Instruments
2    Author: Shubhrajyoti Datta <shubhrajyoti@ti.com>
3    Acknowledgement: Jonathan Cameron <jic23@cam.ac.uk> for valuable inputs.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/i2c.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25#include "../iio.h"
26#include "../sysfs.h"
27
28#define HMC5843_I2C_ADDRESS			0x1E
29
30#define HMC5843_CONFIG_REG_A			0x00
31#define HMC5843_CONFIG_REG_B			0x01
32#define HMC5843_MODE_REG			0x02
33#define HMC5843_DATA_OUT_X_MSB_REG		0x03
34#define HMC5843_DATA_OUT_X_LSB_REG		0x04
35#define HMC5843_DATA_OUT_Y_MSB_REG		0x05
36#define HMC5843_DATA_OUT_Y_LSB_REG		0x06
37#define HMC5843_DATA_OUT_Z_MSB_REG		0x07
38#define HMC5843_DATA_OUT_Z_LSB_REG		0x08
39#define HMC5843_STATUS_REG			0x09
40#define HMC5843_ID_REG_A			0x0A
41#define HMC5843_ID_REG_B			0x0B
42#define HMC5843_ID_REG_C			0x0C
43
44#define HMC5843_ID_REG_LENGTH			0x03
45#define HMC5843_ID_STRING			"H43"
46
47/*
48 * Range settings in  (+-)Ga
49 * */
50#define RANGE_GAIN_OFFSET			0x05
51
52#define	RANGE_0_7				0x00
53#define	RANGE_1_0				0x01 /* default */
54#define	RANGE_1_5				0x02
55#define	RANGE_2_0				0x03
56#define	RANGE_3_2				0x04
57#define	RANGE_3_8				0x05
58#define	RANGE_4_5				0x06
59#define	RANGE_6_5				0x07 /* Not recommended */
60
61/*
62 * Device status
63 */
64#define	DATA_READY				0x01
65#define	DATA_OUTPUT_LOCK			0x02
66#define	VOLTAGE_REGULATOR_ENABLED		0x04
67
68/*
69 * Mode register configuration
70 */
71#define	MODE_CONVERSION_CONTINUOUS		0x00
72#define	MODE_CONVERSION_SINGLE			0x01
73#define	MODE_IDLE				0x02
74#define	MODE_SLEEP				0x03
75
76/* Minimum Data Output Rate in 1/10 Hz  */
77#define RATE_OFFSET				0x02
78#define RATE_BITMASK				0x1C
79#define	RATE_5					0x00
80#define	RATE_10					0x01
81#define	RATE_20					0x02
82#define	RATE_50					0x03
83#define	RATE_100				0x04
84#define	RATE_200				0x05
85#define	RATE_500				0x06
86#define	RATE_NOT_USED				0x07
87
88/*
89 * Device Configuration
90 */
91#define	CONF_NORMAL				0x00
92#define	CONF_POSITIVE_BIAS			0x01
93#define	CONF_NEGATIVE_BIAS			0x02
94#define	CONF_NOT_USED				0x03
95#define	MEAS_CONF_MASK				0x03
96
97static int hmc5843_regval_to_nanoscale[] = {
98	6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
99};
100
101static const int regval_to_input_field_mg[] = {
102	700,
103	1000,
104	1500,
105	2000,
106	3200,
107	3800,
108	4500,
109	6500
110};
111static const char * const regval_to_samp_freq[] = {
112	"0.5",
113	"1",
114	"2",
115	"5",
116	"10",
117	"20",
118	"50",
119};
120
121/* Addresses to scan: 0x1E */
122static const unsigned short normal_i2c[] = { HMC5843_I2C_ADDRESS,
123							I2C_CLIENT_END };
124
125/* Each client has this additional data */
126struct hmc5843_data {
127	struct mutex lock;
128	u8		rate;
129	u8		meas_conf;
130	u8		operating_mode;
131	u8		range;
132};
133
134static void hmc5843_init_client(struct i2c_client *client);
135
136static s32 hmc5843_configure(struct i2c_client *client,
137				       u8 operating_mode)
138{
139	/* The lower two bits contain the current conversion mode */
140	return i2c_smbus_write_byte_data(client,
141					HMC5843_MODE_REG,
142					(operating_mode & 0x03));
143}
144
145/* Return the measurement value from the specified channel */
146static int hmc5843_read_measurement(struct iio_dev *indio_dev,
147				    int address,
148				    int *val)
149{
150	struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
151	struct hmc5843_data *data = iio_priv(indio_dev);
152	s32 result;
153
154	mutex_lock(&data->lock);
155	result = i2c_smbus_read_byte_data(client, HMC5843_STATUS_REG);
156	while (!(result & DATA_READY))
157		result = i2c_smbus_read_byte_data(client, HMC5843_STATUS_REG);
158
159	result = i2c_smbus_read_word_data(client, address);
160	mutex_unlock(&data->lock);
161	if (result < 0)
162		return -EINVAL;
163
164	*val	= (s16)swab16((u16)result);
165	return IIO_VAL_INT;
166}
167
168
169/*
170 * From the datasheet
171 * 0 - Continuous-Conversion Mode: In continuous-conversion mode, the
172 * device continuously performs conversions and places the result in the
173 * data register.
174 *
175 * 1 - Single-Conversion Mode : device performs a single measurement,
176 *  sets RDY high and returned to sleep mode
177 *
178 * 2 - Idle Mode :  Device is placed in idle mode.
179 *
180 * 3 - Sleep Mode. Device is placed in sleep mode.
181 *
182 */
183static ssize_t hmc5843_show_operating_mode(struct device *dev,
184					struct device_attribute *attr,
185					char *buf)
186{
187	struct iio_dev *indio_dev = dev_get_drvdata(dev);
188	struct hmc5843_data *data = iio_priv(indio_dev);
189	return sprintf(buf, "%d\n", data->operating_mode);
190}
191
192static ssize_t hmc5843_set_operating_mode(struct device *dev,
193				struct device_attribute *attr,
194				const char *buf,
195				size_t count)
196{
197	struct iio_dev *indio_dev = dev_get_drvdata(dev);
198	struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
199	struct hmc5843_data *data = iio_priv(indio_dev);
200	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
201	unsigned long operating_mode = 0;
202	s32 status;
203	int error;
204	mutex_lock(&data->lock);
205	error = strict_strtoul(buf, 10, &operating_mode);
206	if (error) {
207		count = error;
208		goto exit;
209	}
210	dev_dbg(dev, "set Conversion mode to %lu\n", operating_mode);
211	if (operating_mode > MODE_SLEEP) {
212		count = -EINVAL;
213		goto exit;
214	}
215
216	status = i2c_smbus_write_byte_data(client, this_attr->address,
217					operating_mode);
218	if (status) {
219		count = -EINVAL;
220		goto exit;
221	}
222	data->operating_mode = operating_mode;
223
224exit:
225	mutex_unlock(&data->lock);
226	return count;
227}
228static IIO_DEVICE_ATTR(operating_mode,
229			S_IWUSR | S_IRUGO,
230			hmc5843_show_operating_mode,
231			hmc5843_set_operating_mode,
232			HMC5843_MODE_REG);
233
234/*
235 * API for setting the measurement configuration to
236 * Normal, Positive bias and Negative bias
237 * From the datasheet
238 *
239 * Normal measurement configuration (default): In normal measurement
240 * configuration the device follows normal measurement flow. Pins BP and BN
241 * are left floating and high impedance.
242 *
243 * Positive bias configuration: In positive bias configuration, a positive
244 * current is forced across the resistive load on pins BP and BN.
245 *
246 * Negative bias configuration. In negative bias configuration, a negative
247 * current is forced across the resistive load on pins BP and BN.
248 *
249 */
250static s32 hmc5843_set_meas_conf(struct i2c_client *client,
251				      u8 meas_conf)
252{
253	struct hmc5843_data *data = i2c_get_clientdata(client);
254	u8 reg_val;
255	reg_val = (meas_conf & MEAS_CONF_MASK) |  (data->rate << RATE_OFFSET);
256	return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
257}
258
259static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
260						struct device_attribute *attr,
261						char *buf)
262{
263	struct iio_dev *indio_dev = dev_get_drvdata(dev);
264	struct hmc5843_data *data = iio_priv(indio_dev);
265	return sprintf(buf, "%d\n", data->meas_conf);
266}
267
268static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
269						struct device_attribute *attr,
270						const char *buf,
271						size_t count)
272{
273	struct iio_dev *indio_dev = dev_get_drvdata(dev);
274	struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
275	struct hmc5843_data *data = i2c_get_clientdata(client);
276	unsigned long meas_conf = 0;
277	int error = strict_strtoul(buf, 10, &meas_conf);
278	if (error)
279		return error;
280	mutex_lock(&data->lock);
281
282	dev_dbg(dev, "set mode to %lu\n", meas_conf);
283	if (hmc5843_set_meas_conf(client, meas_conf)) {
284		count = -EINVAL;
285		goto exit;
286	}
287	data->meas_conf = meas_conf;
288
289exit:
290	mutex_unlock(&data->lock);
291	return count;
292}
293static IIO_DEVICE_ATTR(meas_conf,
294			S_IWUSR | S_IRUGO,
295			hmc5843_show_measurement_configuration,
296			hmc5843_set_measurement_configuration,
297			0);
298
299/*
300 * From Datasheet
301 * The table shows the minimum data output
302 * Value	| Minimum data output rate(Hz)
303 * 0		| 0.5
304 * 1		| 1
305 * 2		| 2
306 * 3		| 5
307 * 4		| 10 (default)
308 * 5		| 20
309 * 6		| 50
310 * 7		| Not used
311 */
312static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("0.5 1 2 5 10 20 50");
313
314static s32 hmc5843_set_rate(struct i2c_client *client,
315				u8 rate)
316{
317	struct hmc5843_data *data = i2c_get_clientdata(client);
318	u8 reg_val;
319
320	reg_val = (data->meas_conf) |  (rate << RATE_OFFSET);
321	if (rate >= RATE_NOT_USED) {
322		dev_err(&client->dev,
323			"This data output rate is not supported\n");
324		return -EINVAL;
325	}
326	return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
327}
328
329static ssize_t set_sampling_frequency(struct device *dev,
330					struct device_attribute *attr,
331					const char *buf, size_t count)
332{
333
334	struct iio_dev *indio_dev = dev_get_drvdata(dev);
335	struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
336	struct hmc5843_data *data = iio_priv(indio_dev);
337	unsigned long rate = 0;
338
339	if (strncmp(buf, "0.5" , 3) == 0)
340		rate = RATE_5;
341	else if (strncmp(buf, "1" , 1) == 0)
342		rate = RATE_10;
343	else if (strncmp(buf, "2", 1) == 0)
344		rate = RATE_20;
345	else if (strncmp(buf, "5", 1) == 0)
346		rate = RATE_50;
347	else if (strncmp(buf, "10", 2) == 0)
348		rate = RATE_100;
349	else if (strncmp(buf, "20" , 2) == 0)
350		rate = RATE_200;
351	else if (strncmp(buf, "50" , 2) == 0)
352		rate = RATE_500;
353	else
354		return -EINVAL;
355
356	mutex_lock(&data->lock);
357	dev_dbg(dev, "set rate to %lu\n", rate);
358	if (hmc5843_set_rate(client, rate)) {
359		count = -EINVAL;
360		goto exit;
361	}
362	data->rate = rate;
363
364exit:
365	mutex_unlock(&data->lock);
366	return count;
367}
368
369static ssize_t show_sampling_frequency(struct device *dev,
370			struct device_attribute *attr, char *buf)
371{
372	struct iio_dev *indio_dev = dev_get_drvdata(dev);
373	struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
374	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
375	s32 rate;
376
377	rate = i2c_smbus_read_byte_data(client,  this_attr->address);
378	if (rate < 0)
379		return rate;
380	rate = (rate & RATE_BITMASK) >> RATE_OFFSET;
381	return sprintf(buf, "%s\n", regval_to_samp_freq[rate]);
382}
383static IIO_DEVICE_ATTR(sampling_frequency,
384			S_IWUSR | S_IRUGO,
385			show_sampling_frequency,
386			set_sampling_frequency,
387			HMC5843_CONFIG_REG_A);
388
389/*
390 * From Datasheet
391 *	Nominal gain settings
392 * Value	| Sensor Input Field Range(Ga)	| Gain(counts/ milli-gauss)
393 *0		|(+-)0.7			|1620
394 *1		|(+-)1.0			|1300
395 *2		|(+-)1.5			|970
396 *3		|(+-)2.0			|780
397 *4		|(+-)3.2			|530
398 *5		|(+-)3.8			|460
399 *6		|(+-)4.5			|390
400 *7		|(+-)6.5			|280
401 */
402static ssize_t show_range(struct device *dev,
403				struct device_attribute *attr,
404				char *buf)
405{
406	u8 range;
407	struct iio_dev *indio_dev = dev_get_drvdata(dev);
408	struct hmc5843_data *data = iio_priv(indio_dev);
409
410	range = data->range;
411	return sprintf(buf, "%d\n", regval_to_input_field_mg[range]);
412}
413
414static ssize_t set_range(struct device *dev,
415			struct device_attribute *attr,
416			const char *buf,
417			size_t count)
418{
419	struct iio_dev *indio_dev = dev_get_drvdata(dev);
420	struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
421	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
422	struct hmc5843_data *data = iio_priv(indio_dev);
423	unsigned long range = 0;
424	int error;
425	mutex_lock(&data->lock);
426	error = strict_strtoul(buf, 10, &range);
427	if (error) {
428		count = error;
429		goto exit;
430	}
431	dev_dbg(dev, "set range to %lu\n", range);
432
433	if (range > RANGE_6_5) {
434		count = -EINVAL;
435		goto exit;
436	}
437
438	data->range = range;
439	range = range << RANGE_GAIN_OFFSET;
440	if (i2c_smbus_write_byte_data(client, this_attr->address, range))
441		count = -EINVAL;
442
443exit:
444	mutex_unlock(&data->lock);
445	return count;
446
447}
448static IIO_DEVICE_ATTR(in_magn_range,
449			S_IWUSR | S_IRUGO,
450			show_range,
451			set_range,
452			HMC5843_CONFIG_REG_B);
453
454static int hmc5843_read_raw(struct iio_dev *indio_dev,
455			    struct iio_chan_spec const *chan,
456			    int *val, int *val2,
457			    long mask)
458{
459	struct hmc5843_data *data = iio_priv(indio_dev);
460
461	switch (mask) {
462	case 0:
463		return hmc5843_read_measurement(indio_dev,
464						chan->address,
465						val);
466	case IIO_CHAN_INFO_SCALE:
467		*val = 0;
468		*val2 = hmc5843_regval_to_nanoscale[data->range];
469		return IIO_VAL_INT_PLUS_NANO;
470	};
471	return -EINVAL;
472}
473
474#define HMC5843_CHANNEL(axis, add)					\
475	{								\
476		.type = IIO_MAGN,					\
477		.modified = 1,						\
478		.channel2 = IIO_MOD_##axis,				\
479		.info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,		\
480		.address = add						\
481	}
482
483static const struct iio_chan_spec hmc5843_channels[] = {
484	HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
485	HMC5843_CHANNEL(Y, HMC5843_DATA_OUT_Y_MSB_REG),
486	HMC5843_CHANNEL(Z, HMC5843_DATA_OUT_Z_MSB_REG),
487};
488
489static struct attribute *hmc5843_attributes[] = {
490	&iio_dev_attr_meas_conf.dev_attr.attr,
491	&iio_dev_attr_operating_mode.dev_attr.attr,
492	&iio_dev_attr_sampling_frequency.dev_attr.attr,
493	&iio_dev_attr_in_magn_range.dev_attr.attr,
494	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
495	NULL
496};
497
498static const struct attribute_group hmc5843_group = {
499	.attrs = hmc5843_attributes,
500};
501
502static int hmc5843_detect(struct i2c_client *client,
503			  struct i2c_board_info *info)
504{
505	unsigned char id_str[HMC5843_ID_REG_LENGTH];
506
507	if (client->addr != HMC5843_I2C_ADDRESS)
508		return -ENODEV;
509
510	if (i2c_smbus_read_i2c_block_data(client, HMC5843_ID_REG_A,
511				HMC5843_ID_REG_LENGTH, id_str)
512			!= HMC5843_ID_REG_LENGTH)
513		return -ENODEV;
514
515	if (0 != strncmp(id_str, HMC5843_ID_STRING, HMC5843_ID_REG_LENGTH))
516		return -ENODEV;
517
518	return 0;
519}
520
521/* Called when we have found a new HMC5843. */
522static void hmc5843_init_client(struct i2c_client *client)
523{
524	struct iio_dev *indio_dev = i2c_get_clientdata(client);
525	struct hmc5843_data *data = iio_priv(indio_dev);
526
527	hmc5843_set_meas_conf(client, data->meas_conf);
528	hmc5843_set_rate(client, data->rate);
529	hmc5843_configure(client, data->operating_mode);
530	i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_B, data->range);
531	mutex_init(&data->lock);
532	pr_info("HMC5843 initialized\n");
533}
534
535static const struct iio_info hmc5843_info = {
536	.attrs = &hmc5843_group,
537	.read_raw = &hmc5843_read_raw,
538	.driver_module = THIS_MODULE,
539};
540
541static int hmc5843_probe(struct i2c_client *client,
542			 const struct i2c_device_id *id)
543{
544	struct hmc5843_data *data;
545	struct iio_dev *indio_dev;
546	int err = 0;
547
548	indio_dev = iio_allocate_device(sizeof(*data));
549	if (indio_dev == NULL) {
550		err = -ENOMEM;
551		goto exit;
552	}
553	data = iio_priv(indio_dev);
554	/* default settings at probe */
555
556	data->meas_conf = CONF_NORMAL;
557	data->range = RANGE_1_0;
558	data->operating_mode = MODE_CONVERSION_CONTINUOUS;
559
560	i2c_set_clientdata(client, indio_dev);
561
562	/* Initialize the HMC5843 chip */
563	hmc5843_init_client(client);
564
565	indio_dev->info = &hmc5843_info;
566	indio_dev->name = id->name;
567	indio_dev->channels = hmc5843_channels;
568	indio_dev->num_channels = ARRAY_SIZE(hmc5843_channels);
569	indio_dev->dev.parent = &client->dev;
570	indio_dev->modes = INDIO_DIRECT_MODE;
571	err = iio_device_register(indio_dev);
572	if (err)
573		goto exit_free2;
574	return 0;
575exit_free2:
576	iio_free_device(indio_dev);
577exit:
578	return err;
579}
580
581static int hmc5843_remove(struct i2c_client *client)
582{
583	struct iio_dev *indio_dev = i2c_get_clientdata(client);
584
585	iio_device_unregister(indio_dev);
586	 /*  sleep mode to save power */
587	hmc5843_configure(client, MODE_SLEEP);
588	iio_free_device(indio_dev);
589
590	return 0;
591}
592
593#ifdef CONFIG_PM_SLEEP
594static int hmc5843_suspend(struct device *dev)
595{
596	hmc5843_configure(to_i2c_client(dev), MODE_SLEEP);
597	return 0;
598}
599
600static int hmc5843_resume(struct device *dev)
601{
602	struct hmc5843_data *data = i2c_get_clientdata(to_i2c_client(dev));
603	hmc5843_configure(to_i2c_client(dev), data->operating_mode);
604	return 0;
605}
606
607static SIMPLE_DEV_PM_OPS(hmc5843_pm_ops, hmc5843_suspend, hmc5843_resume);
608#define HMC5843_PM_OPS (&hmc5843_pm_ops)
609#else
610#define HMC5843_PM_OPS NULL
611#endif
612
613static const struct i2c_device_id hmc5843_id[] = {
614	{ "hmc5843", 0 },
615	{ }
616};
617MODULE_DEVICE_TABLE(i2c, hmc5843_id);
618
619static struct i2c_driver hmc5843_driver = {
620	.driver = {
621		.name	= "hmc5843",
622		.pm	= HMC5843_PM_OPS,
623	},
624	.id_table	= hmc5843_id,
625	.probe		= hmc5843_probe,
626	.remove		= hmc5843_remove,
627	.detect		= hmc5843_detect,
628	.address_list	= normal_i2c,
629};
630module_i2c_driver(hmc5843_driver);
631
632MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com");
633MODULE_DESCRIPTION("HMC5843 driver");
634MODULE_LICENSE("GPL");
635