1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ADLX345/346 Three-Axis Digital Accelerometers (I2C Interface)
5 * Enter bugs at http://blackfin.uclinux.org/
7 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
10 #include <linux/input.h> /* BUS_I2C */
11 #include <linux/i2c.h>
12 #include <linux/module.h>
14 #include <linux/types.h>
18 static int adxl34x_smbus_read(struct device
*dev
, unsigned char reg
)
20 struct i2c_client
*client
= to_i2c_client(dev
);
22 return i2c_smbus_read_byte_data(client
, reg
);
25 static int adxl34x_smbus_write(struct device
*dev
,
26 unsigned char reg
, unsigned char val
)
28 struct i2c_client
*client
= to_i2c_client(dev
);
30 return i2c_smbus_write_byte_data(client
, reg
, val
);
33 static int adxl34x_smbus_read_block(struct device
*dev
,
34 unsigned char reg
, int count
,
37 struct i2c_client
*client
= to_i2c_client(dev
);
39 return i2c_smbus_read_i2c_block_data(client
, reg
, count
, buf
);
42 static int adxl34x_i2c_read_block(struct device
*dev
,
43 unsigned char reg
, int count
,
46 struct i2c_client
*client
= to_i2c_client(dev
);
49 ret
= i2c_master_send(client
, ®
, 1);
53 ret
= i2c_master_recv(client
, buf
, count
);
63 static const struct adxl34x_bus_ops adxl34x_smbus_bops
= {
65 .write
= adxl34x_smbus_write
,
66 .read
= adxl34x_smbus_read
,
67 .read_block
= adxl34x_smbus_read_block
,
70 static const struct adxl34x_bus_ops adxl34x_i2c_bops
= {
72 .write
= adxl34x_smbus_write
,
73 .read
= adxl34x_smbus_read
,
74 .read_block
= adxl34x_i2c_read_block
,
77 static int adxl34x_i2c_probe(struct i2c_client
*client
,
78 const struct i2c_device_id
*id
)
83 error
= i2c_check_functionality(client
->adapter
,
84 I2C_FUNC_SMBUS_BYTE_DATA
);
86 dev_err(&client
->dev
, "SMBUS Byte Data not Supported\n");
90 ac
= adxl34x_probe(&client
->dev
, client
->irq
, false,
91 i2c_check_functionality(client
->adapter
,
92 I2C_FUNC_SMBUS_READ_I2C_BLOCK
) ?
93 &adxl34x_smbus_bops
: &adxl34x_i2c_bops
);
97 i2c_set_clientdata(client
, ac
);
102 static int adxl34x_i2c_remove(struct i2c_client
*client
)
104 struct adxl34x
*ac
= i2c_get_clientdata(client
);
106 return adxl34x_remove(ac
);
109 static int __maybe_unused
adxl34x_i2c_suspend(struct device
*dev
)
111 struct i2c_client
*client
= to_i2c_client(dev
);
112 struct adxl34x
*ac
= i2c_get_clientdata(client
);
119 static int __maybe_unused
adxl34x_i2c_resume(struct device
*dev
)
121 struct i2c_client
*client
= to_i2c_client(dev
);
122 struct adxl34x
*ac
= i2c_get_clientdata(client
);
129 static SIMPLE_DEV_PM_OPS(adxl34x_i2c_pm
, adxl34x_i2c_suspend
,
132 static const struct i2c_device_id adxl34x_id
[] = {
137 MODULE_DEVICE_TABLE(i2c
, adxl34x_id
);
139 static const struct of_device_id adxl34x_of_id
[] = {
141 * The ADXL346 is backward-compatible with the ADXL345. Differences are
142 * handled by runtime detection of the device model, there's thus no
143 * need for listing the "adi,adxl346" compatible value explicitly.
145 { .compatible
= "adi,adxl345", },
147 * Deprecated, DT nodes should use one or more of the device-specific
148 * compatible values "adi,adxl345" and "adi,adxl346".
150 { .compatible
= "adi,adxl34x", },
154 MODULE_DEVICE_TABLE(of
, adxl34x_of_id
);
156 static struct i2c_driver adxl34x_driver
= {
159 .pm
= &adxl34x_i2c_pm
,
160 .of_match_table
= adxl34x_of_id
,
162 .probe
= adxl34x_i2c_probe
,
163 .remove
= adxl34x_i2c_remove
,
164 .id_table
= adxl34x_id
,
167 module_i2c_driver(adxl34x_driver
);
169 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
170 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
171 MODULE_LICENSE("GPL");