blob: e64368a633466bf695913cd5dff1e43747eaba42 [file] [log] [blame]
Thomas Gleixner80503b22019-05-24 12:04:09 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Michael Henneriche27c7292010-06-25 08:44:10 -07002/*
3 * ADLX345/346 Three-Axis Digital Accelerometers (I2C Interface)
4 *
5 * Enter bugs at http://blackfin.uclinux.org/
6 *
7 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
Michael Henneriche27c7292010-06-25 08:44:10 -07008 */
9
10#include <linux/input.h> /* BUS_I2C */
11#include <linux/i2c.h>
12#include <linux/module.h>
Laurent Pinchart3a389582015-05-21 16:07:37 -070013#include <linux/of.h>
Michael Henneriche27c7292010-06-25 08:44:10 -070014#include <linux/types.h>
Mark Brownfbb89932011-02-11 08:51:52 -080015#include <linux/pm.h>
Michael Henneriche27c7292010-06-25 08:44:10 -070016#include "adxl34x.h"
17
18static int adxl34x_smbus_read(struct device *dev, unsigned char reg)
19{
20 struct i2c_client *client = to_i2c_client(dev);
21
22 return i2c_smbus_read_byte_data(client, reg);
23}
24
25static int adxl34x_smbus_write(struct device *dev,
26 unsigned char reg, unsigned char val)
27{
28 struct i2c_client *client = to_i2c_client(dev);
29
30 return i2c_smbus_write_byte_data(client, reg, val);
31}
32
33static int adxl34x_smbus_read_block(struct device *dev,
34 unsigned char reg, int count,
35 void *buf)
36{
37 struct i2c_client *client = to_i2c_client(dev);
38
39 return i2c_smbus_read_i2c_block_data(client, reg, count, buf);
40}
41
42static int adxl34x_i2c_read_block(struct device *dev,
43 unsigned char reg, int count,
44 void *buf)
45{
46 struct i2c_client *client = to_i2c_client(dev);
47 int ret;
48
49 ret = i2c_master_send(client, &reg, 1);
50 if (ret < 0)
51 return ret;
52
53 ret = i2c_master_recv(client, buf, count);
54 if (ret < 0)
55 return ret;
56
57 if (ret != count)
58 return -EIO;
59
60 return 0;
61}
62
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -070063static const struct adxl34x_bus_ops adxl34x_smbus_bops = {
Michael Henneriche27c7292010-06-25 08:44:10 -070064 .bustype = BUS_I2C,
65 .write = adxl34x_smbus_write,
66 .read = adxl34x_smbus_read,
67 .read_block = adxl34x_smbus_read_block,
68};
69
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -070070static const struct adxl34x_bus_ops adxl34x_i2c_bops = {
Michael Henneriche27c7292010-06-25 08:44:10 -070071 .bustype = BUS_I2C,
72 .write = adxl34x_smbus_write,
73 .read = adxl34x_smbus_read,
74 .read_block = adxl34x_i2c_read_block,
75};
76
Bill Pemberton5298cc42012-11-23 21:38:25 -080077static int adxl34x_i2c_probe(struct i2c_client *client,
Michael Henneriche27c7292010-06-25 08:44:10 -070078 const struct i2c_device_id *id)
79{
80 struct adxl34x *ac;
81 int error;
82
83 error = i2c_check_functionality(client->adapter,
84 I2C_FUNC_SMBUS_BYTE_DATA);
85 if (!error) {
86 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
87 return -EIO;
88 }
89
90 ac = adxl34x_probe(&client->dev, client->irq, false,
91 i2c_check_functionality(client->adapter,
92 I2C_FUNC_SMBUS_READ_I2C_BLOCK) ?
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -070093 &adxl34x_smbus_bops : &adxl34x_i2c_bops);
Michael Henneriche27c7292010-06-25 08:44:10 -070094 if (IS_ERR(ac))
95 return PTR_ERR(ac);
96
97 i2c_set_clientdata(client, ac);
98
99 return 0;
100}
101
Bill Pembertone2619cf2012-11-23 21:50:47 -0800102static int adxl34x_i2c_remove(struct i2c_client *client)
Michael Henneriche27c7292010-06-25 08:44:10 -0700103{
104 struct adxl34x *ac = i2c_get_clientdata(client);
105
106 return adxl34x_remove(ac);
107}
108
Jingoo Han97a652a2014-11-02 00:02:46 -0700109static int __maybe_unused adxl34x_i2c_suspend(struct device *dev)
Michael Henneriche27c7292010-06-25 08:44:10 -0700110{
Mark Brownfbb89932011-02-11 08:51:52 -0800111 struct i2c_client *client = to_i2c_client(dev);
Michael Henneriche27c7292010-06-25 08:44:10 -0700112 struct adxl34x *ac = i2c_get_clientdata(client);
113
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -0700114 adxl34x_suspend(ac);
Michael Henneriche27c7292010-06-25 08:44:10 -0700115
116 return 0;
117}
118
Jingoo Han97a652a2014-11-02 00:02:46 -0700119static int __maybe_unused adxl34x_i2c_resume(struct device *dev)
Michael Henneriche27c7292010-06-25 08:44:10 -0700120{
Mark Brownfbb89932011-02-11 08:51:52 -0800121 struct i2c_client *client = to_i2c_client(dev);
Michael Henneriche27c7292010-06-25 08:44:10 -0700122 struct adxl34x *ac = i2c_get_clientdata(client);
123
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -0700124 adxl34x_resume(ac);
Michael Henneriche27c7292010-06-25 08:44:10 -0700125
126 return 0;
127}
Michael Henneriche27c7292010-06-25 08:44:10 -0700128
Mark Brownfbb89932011-02-11 08:51:52 -0800129static SIMPLE_DEV_PM_OPS(adxl34x_i2c_pm, adxl34x_i2c_suspend,
130 adxl34x_i2c_resume);
131
Michael Henneriche27c7292010-06-25 08:44:10 -0700132static const struct i2c_device_id adxl34x_id[] = {
133 { "adxl34x", 0 },
134 { }
135};
136
137MODULE_DEVICE_TABLE(i2c, adxl34x_id);
138
Laurent Pinchart3a389582015-05-21 16:07:37 -0700139static const struct of_device_id adxl34x_of_id[] = {
140 /*
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.
144 */
145 { .compatible = "adi,adxl345", },
146 /*
147 * Deprecated, DT nodes should use one or more of the device-specific
148 * compatible values "adi,adxl345" and "adi,adxl346".
149 */
150 { .compatible = "adi,adxl34x", },
151 { }
152};
153
154MODULE_DEVICE_TABLE(of, adxl34x_of_id);
Laurent Pinchart3a389582015-05-21 16:07:37 -0700155
Michael Henneriche27c7292010-06-25 08:44:10 -0700156static struct i2c_driver adxl34x_driver = {
157 .driver = {
158 .name = "adxl34x",
Mark Brownfbb89932011-02-11 08:51:52 -0800159 .pm = &adxl34x_i2c_pm,
Andy Shevchenko01427fe2017-01-03 11:51:48 -0800160 .of_match_table = adxl34x_of_id,
Michael Henneriche27c7292010-06-25 08:44:10 -0700161 },
162 .probe = adxl34x_i2c_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800163 .remove = adxl34x_i2c_remove,
Michael Henneriche27c7292010-06-25 08:44:10 -0700164 .id_table = adxl34x_id,
165};
166
Axel Lin1b92c1c2012-03-16 23:05:41 -0700167module_i2c_driver(adxl34x_driver);
Michael Henneriche27c7292010-06-25 08:44:10 -0700168
169MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
170MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
171MODULE_LICENSE("GPL");