blob: 470bfd6f08304c67ca7934871d2849605b0163b4 [file] [log] [blame]
Michael Henneriche27c7292010-06-25 08:44:10 -07001/*
2 * ADLX345/346 Three-Axis Digital Accelerometers (I2C Interface)
3 *
4 * Enter bugs at http://blackfin.uclinux.org/
5 *
6 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
8 */
9
10#include <linux/input.h> /* BUS_I2C */
11#include <linux/i2c.h>
12#include <linux/module.h>
13#include <linux/types.h>
Mark Brownfbb89932011-02-11 08:51:52 -080014#include <linux/pm.h>
Michael Henneriche27c7292010-06-25 08:44:10 -070015#include "adxl34x.h"
16
17static int adxl34x_smbus_read(struct device *dev, unsigned char reg)
18{
19 struct i2c_client *client = to_i2c_client(dev);
20
21 return i2c_smbus_read_byte_data(client, reg);
22}
23
24static int adxl34x_smbus_write(struct device *dev,
25 unsigned char reg, unsigned char val)
26{
27 struct i2c_client *client = to_i2c_client(dev);
28
29 return i2c_smbus_write_byte_data(client, reg, val);
30}
31
32static int adxl34x_smbus_read_block(struct device *dev,
33 unsigned char reg, int count,
34 void *buf)
35{
36 struct i2c_client *client = to_i2c_client(dev);
37
38 return i2c_smbus_read_i2c_block_data(client, reg, count, buf);
39}
40
41static int adxl34x_i2c_read_block(struct device *dev,
42 unsigned char reg, int count,
43 void *buf)
44{
45 struct i2c_client *client = to_i2c_client(dev);
46 int ret;
47
48 ret = i2c_master_send(client, &reg, 1);
49 if (ret < 0)
50 return ret;
51
52 ret = i2c_master_recv(client, buf, count);
53 if (ret < 0)
54 return ret;
55
56 if (ret != count)
57 return -EIO;
58
59 return 0;
60}
61
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -070062static const struct adxl34x_bus_ops adxl34x_smbus_bops = {
Michael Henneriche27c7292010-06-25 08:44:10 -070063 .bustype = BUS_I2C,
64 .write = adxl34x_smbus_write,
65 .read = adxl34x_smbus_read,
66 .read_block = adxl34x_smbus_read_block,
67};
68
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -070069static const struct adxl34x_bus_ops adxl34x_i2c_bops = {
Michael Henneriche27c7292010-06-25 08:44:10 -070070 .bustype = BUS_I2C,
71 .write = adxl34x_smbus_write,
72 .read = adxl34x_smbus_read,
73 .read_block = adxl34x_i2c_read_block,
74};
75
Bill Pemberton5298cc42012-11-23 21:38:25 -080076static int adxl34x_i2c_probe(struct i2c_client *client,
Michael Henneriche27c7292010-06-25 08:44:10 -070077 const struct i2c_device_id *id)
78{
79 struct adxl34x *ac;
80 int error;
81
82 error = i2c_check_functionality(client->adapter,
83 I2C_FUNC_SMBUS_BYTE_DATA);
84 if (!error) {
85 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
86 return -EIO;
87 }
88
89 ac = adxl34x_probe(&client->dev, client->irq, false,
90 i2c_check_functionality(client->adapter,
91 I2C_FUNC_SMBUS_READ_I2C_BLOCK) ?
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -070092 &adxl34x_smbus_bops : &adxl34x_i2c_bops);
Michael Henneriche27c7292010-06-25 08:44:10 -070093 if (IS_ERR(ac))
94 return PTR_ERR(ac);
95
96 i2c_set_clientdata(client, ac);
97
98 return 0;
99}
100
Bill Pembertone2619cf2012-11-23 21:50:47 -0800101static int adxl34x_i2c_remove(struct i2c_client *client)
Michael Henneriche27c7292010-06-25 08:44:10 -0700102{
103 struct adxl34x *ac = i2c_get_clientdata(client);
104
105 return adxl34x_remove(ac);
106}
107
Jingoo Han97a652a2014-11-02 00:02:46 -0700108static int __maybe_unused adxl34x_i2c_suspend(struct device *dev)
Michael Henneriche27c7292010-06-25 08:44:10 -0700109{
Mark Brownfbb89932011-02-11 08:51:52 -0800110 struct i2c_client *client = to_i2c_client(dev);
Michael Henneriche27c7292010-06-25 08:44:10 -0700111 struct adxl34x *ac = i2c_get_clientdata(client);
112
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -0700113 adxl34x_suspend(ac);
Michael Henneriche27c7292010-06-25 08:44:10 -0700114
115 return 0;
116}
117
Jingoo Han97a652a2014-11-02 00:02:46 -0700118static int __maybe_unused adxl34x_i2c_resume(struct device *dev)
Michael Henneriche27c7292010-06-25 08:44:10 -0700119{
Mark Brownfbb89932011-02-11 08:51:52 -0800120 struct i2c_client *client = to_i2c_client(dev);
Michael Henneriche27c7292010-06-25 08:44:10 -0700121 struct adxl34x *ac = i2c_get_clientdata(client);
122
Dmitry Torokhovaf6e1d92010-07-01 09:07:33 -0700123 adxl34x_resume(ac);
Michael Henneriche27c7292010-06-25 08:44:10 -0700124
125 return 0;
126}
Michael Henneriche27c7292010-06-25 08:44:10 -0700127
Mark Brownfbb89932011-02-11 08:51:52 -0800128static SIMPLE_DEV_PM_OPS(adxl34x_i2c_pm, adxl34x_i2c_suspend,
129 adxl34x_i2c_resume);
130
Michael Henneriche27c7292010-06-25 08:44:10 -0700131static const struct i2c_device_id adxl34x_id[] = {
132 { "adxl34x", 0 },
133 { }
134};
135
136MODULE_DEVICE_TABLE(i2c, adxl34x_id);
137
138static struct i2c_driver adxl34x_driver = {
139 .driver = {
140 .name = "adxl34x",
141 .owner = THIS_MODULE,
Mark Brownfbb89932011-02-11 08:51:52 -0800142 .pm = &adxl34x_i2c_pm,
Michael Henneriche27c7292010-06-25 08:44:10 -0700143 },
144 .probe = adxl34x_i2c_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800145 .remove = adxl34x_i2c_remove,
Michael Henneriche27c7292010-06-25 08:44:10 -0700146 .id_table = adxl34x_id,
147};
148
Axel Lin1b92c1c2012-03-16 23:05:41 -0700149module_i2c_driver(adxl34x_driver);
Michael Henneriche27c7292010-06-25 08:44:10 -0700150
151MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
152MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
153MODULE_LICENSE("GPL");