2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
10 #include <linux/i2c.h>
11 #include <linux/rmi.h>
12 #include <linux/irq.h>
14 #include "rmi_driver.h"
16 #define BUFFER_SIZE_INCREMENT 32
19 * struct rmi_i2c_xport - stores information for i2c communication
21 * @xport: The transport interface structure
23 * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
24 * @page: Keeps track of the current virtual page
26 * @tx_buf: Buffer used for transmitting data to the sensor over i2c.
27 * @tx_buf_size: Size of the buffer
29 struct rmi_i2c_xport
{
30 struct rmi_transport_dev xport
;
31 struct i2c_client
*client
;
33 struct mutex page_mutex
;
42 #define RMI_PAGE_SELECT_REGISTER 0xff
43 #define RMI_I2C_PAGE(addr) (((addr) >> 8) & 0xff)
46 * rmi_set_page - Set RMI page
47 * @xport: The pointer to the rmi_transport_dev struct
48 * @page: The new page address.
50 * RMI devices have 16-bit addressing, but some of the transport
51 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
52 * a page address at 0xff of every page so we can reliable page addresses
53 * every 256 registers.
55 * The page_mutex lock must be held when this function is entered.
57 * Returns zero on success, non-zero on failure.
59 static int rmi_set_page(struct rmi_i2c_xport
*rmi_i2c
, u8 page
)
61 struct i2c_client
*client
= rmi_i2c
->client
;
62 u8 txbuf
[2] = {RMI_PAGE_SELECT_REGISTER
, page
};
65 retval
= i2c_master_send(client
, txbuf
, sizeof(txbuf
));
66 if (retval
!= sizeof(txbuf
)) {
68 "%s: set page failed: %d.", __func__
, retval
);
69 return (retval
< 0) ? retval
: -EIO
;
76 static int rmi_i2c_write_block(struct rmi_transport_dev
*xport
, u16 addr
,
77 const void *buf
, size_t len
)
79 struct rmi_i2c_xport
*rmi_i2c
=
80 container_of(xport
, struct rmi_i2c_xport
, xport
);
81 struct i2c_client
*client
= rmi_i2c
->client
;
82 size_t tx_size
= len
+ 1;
85 mutex_lock(&rmi_i2c
->page_mutex
);
87 if (!rmi_i2c
->tx_buf
|| rmi_i2c
->tx_buf_size
< tx_size
) {
89 devm_kfree(&client
->dev
, rmi_i2c
->tx_buf
);
90 rmi_i2c
->tx_buf_size
= tx_size
+ BUFFER_SIZE_INCREMENT
;
91 rmi_i2c
->tx_buf
= devm_kzalloc(&client
->dev
,
94 if (!rmi_i2c
->tx_buf
) {
95 rmi_i2c
->tx_buf_size
= 0;
101 rmi_i2c
->tx_buf
[0] = addr
& 0xff;
102 memcpy(rmi_i2c
->tx_buf
+ 1, buf
, len
);
104 if (RMI_I2C_PAGE(addr
) != rmi_i2c
->page
) {
105 retval
= rmi_set_page(rmi_i2c
, RMI_I2C_PAGE(addr
));
110 retval
= i2c_master_send(client
, rmi_i2c
->tx_buf
, tx_size
);
111 if (retval
== tx_size
)
113 else if (retval
>= 0)
117 rmi_dbg(RMI_DEBUG_XPORT
, &client
->dev
,
118 "write %zd bytes at %#06x: %d (%*ph)\n",
119 len
, addr
, retval
, (int)len
, buf
);
121 mutex_unlock(&rmi_i2c
->page_mutex
);
125 static int rmi_i2c_read_block(struct rmi_transport_dev
*xport
, u16 addr
,
126 void *buf
, size_t len
)
128 struct rmi_i2c_xport
*rmi_i2c
=
129 container_of(xport
, struct rmi_i2c_xport
, xport
);
130 struct i2c_client
*client
= rmi_i2c
->client
;
131 u8 addr_offset
= addr
& 0xff;
133 struct i2c_msg msgs
[] = {
135 .addr
= client
->addr
,
136 .len
= sizeof(addr_offset
),
140 .addr
= client
->addr
,
147 mutex_lock(&rmi_i2c
->page_mutex
);
149 if (RMI_I2C_PAGE(addr
) != rmi_i2c
->page
) {
150 retval
= rmi_set_page(rmi_i2c
, RMI_I2C_PAGE(addr
));
155 retval
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
156 if (retval
== ARRAY_SIZE(msgs
))
157 retval
= 0; /* success */
158 else if (retval
>= 0)
162 rmi_dbg(RMI_DEBUG_XPORT
, &client
->dev
,
163 "read %zd bytes at %#06x: %d (%*ph)\n",
164 len
, addr
, retval
, (int)len
, buf
);
166 mutex_unlock(&rmi_i2c
->page_mutex
);
170 static const struct rmi_transport_ops rmi_i2c_ops
= {
171 .write_block
= rmi_i2c_write_block
,
172 .read_block
= rmi_i2c_read_block
,
175 static irqreturn_t
rmi_i2c_irq(int irq
, void *dev_id
)
177 struct rmi_i2c_xport
*rmi_i2c
= dev_id
;
178 struct rmi_device
*rmi_dev
= rmi_i2c
->xport
.rmi_dev
;
181 ret
= rmi_process_interrupt_requests(rmi_dev
);
183 rmi_dbg(RMI_DEBUG_XPORT
, &rmi_dev
->dev
,
184 "Failed to process interrupt request: %d\n", ret
);
189 static int rmi_i2c_init_irq(struct i2c_client
*client
)
191 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
192 int irq_flags
= irqd_get_trigger_type(irq_get_irq_data(rmi_i2c
->irq
));
196 irq_flags
= IRQF_TRIGGER_LOW
;
198 ret
= devm_request_threaded_irq(&client
->dev
, rmi_i2c
->irq
, NULL
,
199 rmi_i2c_irq
, irq_flags
| IRQF_ONESHOT
, client
->name
,
202 dev_warn(&client
->dev
, "Failed to register interrupt %d\n",
212 static const struct of_device_id rmi_i2c_of_match
[] = {
213 { .compatible
= "syna,rmi4-i2c" },
216 MODULE_DEVICE_TABLE(of
, rmi_i2c_of_match
);
219 static int rmi_i2c_probe(struct i2c_client
*client
,
220 const struct i2c_device_id
*id
)
222 struct rmi_device_platform_data
*pdata
;
223 struct rmi_device_platform_data
*client_pdata
=
224 dev_get_platdata(&client
->dev
);
225 struct rmi_i2c_xport
*rmi_i2c
;
228 rmi_i2c
= devm_kzalloc(&client
->dev
, sizeof(struct rmi_i2c_xport
),
233 pdata
= &rmi_i2c
->xport
.pdata
;
235 if (!client
->dev
.of_node
&& client_pdata
)
236 *pdata
= *client_pdata
;
239 rmi_i2c
->irq
= client
->irq
;
241 rmi_dbg(RMI_DEBUG_XPORT
, &client
->dev
, "Probing %s.\n",
242 dev_name(&client
->dev
));
243 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
244 dev_err(&client
->dev
,
245 "adapter does not support required functionality.\n");
249 rmi_i2c
->client
= client
;
250 mutex_init(&rmi_i2c
->page_mutex
);
252 rmi_i2c
->xport
.dev
= &client
->dev
;
253 rmi_i2c
->xport
.proto_name
= "i2c";
254 rmi_i2c
->xport
.ops
= &rmi_i2c_ops
;
256 i2c_set_clientdata(client
, rmi_i2c
);
259 * Setting the page to zero will (a) make sure the PSR is in a
260 * known state, and (b) make sure we can talk to the device.
262 retval
= rmi_set_page(rmi_i2c
, 0);
264 dev_err(&client
->dev
, "Failed to set page select to 0.\n");
268 retval
= rmi_register_transport_device(&rmi_i2c
->xport
);
270 dev_err(&client
->dev
, "Failed to register transport driver at 0x%.2X.\n",
275 retval
= rmi_i2c_init_irq(client
);
279 dev_info(&client
->dev
, "registered rmi i2c driver at %#04x.\n",
284 static int rmi_i2c_remove(struct i2c_client
*client
)
286 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
288 rmi_unregister_transport_device(&rmi_i2c
->xport
);
293 #ifdef CONFIG_PM_SLEEP
294 static int rmi_i2c_suspend(struct device
*dev
)
296 struct i2c_client
*client
= to_i2c_client(dev
);
297 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
300 ret
= rmi_driver_suspend(rmi_i2c
->xport
.rmi_dev
);
302 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
304 disable_irq(rmi_i2c
->irq
);
305 if (device_may_wakeup(&client
->dev
)) {
306 ret
= enable_irq_wake(rmi_i2c
->irq
);
308 dev_warn(dev
, "Failed to enable irq for wake: %d\n",
314 static int rmi_i2c_resume(struct device
*dev
)
316 struct i2c_client
*client
= to_i2c_client(dev
);
317 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
320 enable_irq(rmi_i2c
->irq
);
321 if (device_may_wakeup(&client
->dev
)) {
322 ret
= disable_irq_wake(rmi_i2c
->irq
);
324 dev_warn(dev
, "Failed to disable irq for wake: %d\n",
328 ret
= rmi_driver_resume(rmi_i2c
->xport
.rmi_dev
);
330 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
337 static int rmi_i2c_runtime_suspend(struct device
*dev
)
339 struct i2c_client
*client
= to_i2c_client(dev
);
340 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
343 ret
= rmi_driver_suspend(rmi_i2c
->xport
.rmi_dev
);
345 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
347 disable_irq(rmi_i2c
->irq
);
352 static int rmi_i2c_runtime_resume(struct device
*dev
)
354 struct i2c_client
*client
= to_i2c_client(dev
);
355 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
358 enable_irq(rmi_i2c
->irq
);
360 ret
= rmi_driver_resume(rmi_i2c
->xport
.rmi_dev
);
362 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
368 static const struct dev_pm_ops rmi_i2c_pm
= {
369 SET_SYSTEM_SLEEP_PM_OPS(rmi_i2c_suspend
, rmi_i2c_resume
)
370 SET_RUNTIME_PM_OPS(rmi_i2c_runtime_suspend
, rmi_i2c_runtime_resume
,
374 static const struct i2c_device_id rmi_id
[] = {
378 MODULE_DEVICE_TABLE(i2c
, rmi_id
);
380 static struct i2c_driver rmi_i2c_driver
= {
384 .of_match_table
= of_match_ptr(rmi_i2c_of_match
),
387 .probe
= rmi_i2c_probe
,
388 .remove
= rmi_i2c_remove
,
391 module_i2c_driver(rmi_i2c_driver
);
393 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>");
394 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
395 MODULE_DESCRIPTION("RMI I2C driver");
396 MODULE_LICENSE("GPL");
397 MODULE_VERSION(RMI_DRIVER_VERSION
);