xref: /openbmc/linux/drivers/iio/accel/da280.c (revision bbecb07f)
1 /**
2  * IIO driver for the MiraMEMS DA280 3-axis accelerometer and
3  * IIO driver for the MiraMEMS DA226 2-axis accelerometer
4  *
5  * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2, as published by the Free Software Foundation.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/byteorder/generic.h>
17 
18 #define DA280_REG_CHIP_ID		0x01
19 #define DA280_REG_ACC_X_LSB		0x02
20 #define DA280_REG_ACC_Y_LSB		0x04
21 #define DA280_REG_ACC_Z_LSB		0x06
22 #define DA280_REG_MODE_BW		0x11
23 
24 #define DA280_CHIP_ID			0x13
25 #define DA280_MODE_ENABLE		0x1e
26 #define DA280_MODE_DISABLE		0x9e
27 
28 enum { da226, da280 };
29 
30 /*
31  * a value of + or -4096 corresponds to + or - 1G
32  * scale = 9.81 / 4096 = 0.002395019
33  */
34 
35 static const int da280_nscale = 2395019;
36 
37 #define DA280_CHANNEL(reg, axis) {	\
38 	.type = IIO_ACCEL,	\
39 	.address = reg,	\
40 	.modified = 1,	\
41 	.channel2 = IIO_MOD_##axis,	\
42 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
43 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
44 }
45 
46 static const struct iio_chan_spec da280_channels[] = {
47 	DA280_CHANNEL(DA280_REG_ACC_X_LSB, X),
48 	DA280_CHANNEL(DA280_REG_ACC_Y_LSB, Y),
49 	DA280_CHANNEL(DA280_REG_ACC_Z_LSB, Z),
50 };
51 
52 struct da280_data {
53 	struct i2c_client *client;
54 };
55 
56 static int da280_enable(struct i2c_client *client, bool enable)
57 {
58 	u8 data = enable ? DA280_MODE_ENABLE : DA280_MODE_DISABLE;
59 
60 	return i2c_smbus_write_byte_data(client, DA280_REG_MODE_BW, data);
61 }
62 
63 static int da280_read_raw(struct iio_dev *indio_dev,
64 				struct iio_chan_spec const *chan,
65 				int *val, int *val2, long mask)
66 {
67 	struct da280_data *data = iio_priv(indio_dev);
68 	int ret;
69 
70 	switch (mask) {
71 	case IIO_CHAN_INFO_RAW:
72 		ret = i2c_smbus_read_word_data(data->client, chan->address);
73 		if (ret < 0)
74 			return ret;
75 		/*
76 		 * Values are 14 bits, stored as 16 bits with the 2
77 		 * least significant bits always 0.
78 		 */
79 		*val = (short)ret >> 2;
80 		return IIO_VAL_INT;
81 	case IIO_CHAN_INFO_SCALE:
82 		*val = 0;
83 		*val2 = da280_nscale;
84 		return IIO_VAL_INT_PLUS_NANO;
85 	default:
86 		return -EINVAL;
87 	}
88 }
89 
90 static const struct iio_info da280_info = {
91 	.read_raw	= da280_read_raw,
92 };
93 
94 static int da280_probe(struct i2c_client *client,
95 			const struct i2c_device_id *id)
96 {
97 	int ret;
98 	struct iio_dev *indio_dev;
99 	struct da280_data *data;
100 
101 	ret = i2c_smbus_read_byte_data(client, DA280_REG_CHIP_ID);
102 	if (ret != DA280_CHIP_ID)
103 		return (ret < 0) ? ret : -ENODEV;
104 
105 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
106 	if (!indio_dev)
107 		return -ENOMEM;
108 
109 	data = iio_priv(indio_dev);
110 	data->client = client;
111 	i2c_set_clientdata(client, indio_dev);
112 
113 	indio_dev->dev.parent = &client->dev;
114 	indio_dev->info = &da280_info;
115 	indio_dev->modes = INDIO_DIRECT_MODE;
116 	indio_dev->channels = da280_channels;
117 	if (id->driver_data == da226) {
118 		indio_dev->name = "da226";
119 		indio_dev->num_channels = 2;
120 	} else {
121 		indio_dev->name = "da280";
122 		indio_dev->num_channels = 3;
123 	}
124 
125 	ret = da280_enable(client, true);
126 	if (ret < 0)
127 		return ret;
128 
129 	ret = iio_device_register(indio_dev);
130 	if (ret < 0) {
131 		dev_err(&client->dev, "device_register failed\n");
132 		da280_enable(client, false);
133 	}
134 
135 	return ret;
136 }
137 
138 static int da280_remove(struct i2c_client *client)
139 {
140 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
141 
142 	iio_device_unregister(indio_dev);
143 
144 	return da280_enable(client, false);
145 }
146 
147 #ifdef CONFIG_PM_SLEEP
148 static int da280_suspend(struct device *dev)
149 {
150 	return da280_enable(to_i2c_client(dev), false);
151 }
152 
153 static int da280_resume(struct device *dev)
154 {
155 	return da280_enable(to_i2c_client(dev), true);
156 }
157 #endif
158 
159 static SIMPLE_DEV_PM_OPS(da280_pm_ops, da280_suspend, da280_resume);
160 
161 static const struct i2c_device_id da280_i2c_id[] = {
162 	{ "da226", da226 },
163 	{ "da280", da280 },
164 	{}
165 };
166 MODULE_DEVICE_TABLE(i2c, da280_i2c_id);
167 
168 static struct i2c_driver da280_driver = {
169 	.driver = {
170 		.name = "da280",
171 		.pm = &da280_pm_ops,
172 	},
173 	.probe		= da280_probe,
174 	.remove		= da280_remove,
175 	.id_table	= da280_i2c_id,
176 };
177 
178 module_i2c_driver(da280_driver);
179 
180 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
181 MODULE_DESCRIPTION("MiraMEMS DA280 3-Axis Accelerometer driver");
182 MODULE_LICENSE("GPL v2");
183