2 * mCube MC3230 3-Axis Accelerometer
4 * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * IIO driver for mCube MC3230; 7-bit I2C address: 0x4c.
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
19 #define MC3230_REG_XOUT 0x00
20 #define MC3230_REG_YOUT 0x01
21 #define MC3230_REG_ZOUT 0x02
23 #define MC3230_REG_MODE 0x07
24 #define MC3230_MODE_OPCON_MASK 0x03
25 #define MC3230_MODE_OPCON_WAKE 0x01
26 #define MC3230_MODE_OPCON_STANDBY 0x03
28 #define MC3230_REG_CHIP_ID 0x18
29 #define MC3230_CHIP_ID 0x01
31 #define MC3230_REG_PRODUCT_CODE 0x3b
32 #define MC3230_PRODUCT_CODE 0x19
35 * The accelerometer has one measurement range:
37 * -1.5g - +1.5g (8-bit, signed)
39 * scale = (1.5 + 1.5) * 9.81 / (2^8 - 1) = 0.115411765
42 static const int mc3230_nscale
= 115411765;
44 #define MC3230_CHANNEL(reg, axis) { \
48 .channel2 = IIO_MOD_##axis, \
49 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
50 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
53 static const struct iio_chan_spec mc3230_channels
[] = {
54 MC3230_CHANNEL(MC3230_REG_XOUT
, X
),
55 MC3230_CHANNEL(MC3230_REG_YOUT
, Y
),
56 MC3230_CHANNEL(MC3230_REG_ZOUT
, Z
),
60 struct i2c_client
*client
;
63 static int mc3230_set_opcon(struct mc3230_data
*data
, int opcon
)
66 struct i2c_client
*client
= data
->client
;
68 ret
= i2c_smbus_read_byte_data(client
, MC3230_REG_MODE
);
70 dev_err(&client
->dev
, "failed to read mode reg: %d\n", ret
);
74 ret
&= ~MC3230_MODE_OPCON_MASK
;
77 ret
= i2c_smbus_write_byte_data(client
, MC3230_REG_MODE
, ret
);
79 dev_err(&client
->dev
, "failed to write mode reg: %d\n", ret
);
86 static int mc3230_read_raw(struct iio_dev
*indio_dev
,
87 struct iio_chan_spec
const *chan
,
88 int *val
, int *val2
, long mask
)
90 struct mc3230_data
*data
= iio_priv(indio_dev
);
94 case IIO_CHAN_INFO_RAW
:
95 ret
= i2c_smbus_read_byte_data(data
->client
, chan
->address
);
98 *val
= sign_extend32(ret
, 7);
100 case IIO_CHAN_INFO_SCALE
:
102 *val2
= mc3230_nscale
;
103 return IIO_VAL_INT_PLUS_NANO
;
109 static const struct iio_info mc3230_info
= {
110 .driver_module
= THIS_MODULE
,
111 .read_raw
= mc3230_read_raw
,
114 static int mc3230_probe(struct i2c_client
*client
,
115 const struct i2c_device_id
*id
)
118 struct iio_dev
*indio_dev
;
119 struct mc3230_data
*data
;
121 /* First check chip-id and product-id */
122 ret
= i2c_smbus_read_byte_data(client
, MC3230_REG_CHIP_ID
);
123 if (ret
!= MC3230_CHIP_ID
)
124 return (ret
< 0) ? ret
: -ENODEV
;
126 ret
= i2c_smbus_read_byte_data(client
, MC3230_REG_PRODUCT_CODE
);
127 if (ret
!= MC3230_PRODUCT_CODE
)
128 return (ret
< 0) ? ret
: -ENODEV
;
130 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
132 dev_err(&client
->dev
, "iio allocation failed!\n");
136 data
= iio_priv(indio_dev
);
137 data
->client
= client
;
138 i2c_set_clientdata(client
, indio_dev
);
140 indio_dev
->dev
.parent
= &client
->dev
;
141 indio_dev
->info
= &mc3230_info
;
142 indio_dev
->name
= "mc3230";
143 indio_dev
->modes
= INDIO_DIRECT_MODE
;
144 indio_dev
->channels
= mc3230_channels
;
145 indio_dev
->num_channels
= ARRAY_SIZE(mc3230_channels
);
147 ret
= mc3230_set_opcon(data
, MC3230_MODE_OPCON_WAKE
);
151 ret
= iio_device_register(indio_dev
);
153 dev_err(&client
->dev
, "device_register failed\n");
154 mc3230_set_opcon(data
, MC3230_MODE_OPCON_STANDBY
);
160 static int mc3230_remove(struct i2c_client
*client
)
162 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
164 iio_device_unregister(indio_dev
);
166 return mc3230_set_opcon(iio_priv(indio_dev
), MC3230_MODE_OPCON_STANDBY
);
169 #ifdef CONFIG_PM_SLEEP
170 static int mc3230_suspend(struct device
*dev
)
172 struct mc3230_data
*data
;
174 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
176 return mc3230_set_opcon(data
, MC3230_MODE_OPCON_STANDBY
);
179 static int mc3230_resume(struct device
*dev
)
181 struct mc3230_data
*data
;
183 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
185 return mc3230_set_opcon(data
, MC3230_MODE_OPCON_WAKE
);
189 static SIMPLE_DEV_PM_OPS(mc3230_pm_ops
, mc3230_suspend
, mc3230_resume
);
191 static const struct i2c_device_id mc3230_i2c_id
[] = {
195 MODULE_DEVICE_TABLE(i2c
, mc3230_i2c_id
);
197 static struct i2c_driver mc3230_driver
= {
200 .pm
= &mc3230_pm_ops
,
202 .probe
= mc3230_probe
,
203 .remove
= mc3230_remove
,
204 .id_table
= mc3230_i2c_id
,
207 module_i2c_driver(mc3230_driver
);
209 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
210 MODULE_DESCRIPTION("mCube MC3230 3-Axis Accelerometer driver");
211 MODULE_LICENSE("GPL v2");