1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2016 Synaptics Incorporated
4 * Copyright (c) 2011 Unixphere
10 #include <linux/delay.h>
11 #include <linux/regulator/consumer.h>
12 #include "rmi_driver.h"
14 #define BUFFER_SIZE_INCREMENT 32
17 * struct rmi_i2c_xport - stores information for i2c communication
19 * @xport: The transport interface structure
21 * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
22 * @page: Keeps track of the current virtual page
24 * @tx_buf: Buffer used for transmitting data to the sensor over i2c.
25 * @tx_buf_size: Size of the buffer
27 struct rmi_i2c_xport
{
28 struct rmi_transport_dev xport
;
29 struct i2c_client
*client
;
31 struct mutex page_mutex
;
37 struct regulator_bulk_data supplies
[2];
41 #define RMI_PAGE_SELECT_REGISTER 0xff
42 #define RMI_I2C_PAGE(addr) (((addr) >> 8) & 0xff)
45 * rmi_set_page - Set RMI page
46 * @xport: The pointer to the rmi_transport_dev struct
47 * @page: The new page address.
49 * RMI devices have 16-bit addressing, but some of the transport
50 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
51 * a page address at 0xff of every page so we can reliable page addresses
52 * every 256 registers.
54 * The page_mutex lock must be held when this function is entered.
56 * Returns zero on success, non-zero on failure.
58 static int rmi_set_page(struct rmi_i2c_xport
*rmi_i2c
, u8 page
)
60 struct i2c_client
*client
= rmi_i2c
->client
;
61 u8 txbuf
[2] = {RMI_PAGE_SELECT_REGISTER
, page
};
64 retval
= i2c_master_send(client
, txbuf
, sizeof(txbuf
));
65 if (retval
!= sizeof(txbuf
)) {
67 "%s: set page failed: %d.", __func__
, retval
);
68 return (retval
< 0) ? retval
: -EIO
;
75 static int rmi_i2c_write_block(struct rmi_transport_dev
*xport
, u16 addr
,
76 const void *buf
, size_t len
)
78 struct rmi_i2c_xport
*rmi_i2c
=
79 container_of(xport
, struct rmi_i2c_xport
, xport
);
80 struct i2c_client
*client
= rmi_i2c
->client
;
81 size_t tx_size
= len
+ 1;
84 mutex_lock(&rmi_i2c
->page_mutex
);
86 if (!rmi_i2c
->tx_buf
|| rmi_i2c
->tx_buf_size
< tx_size
) {
88 devm_kfree(&client
->dev
, rmi_i2c
->tx_buf
);
89 rmi_i2c
->tx_buf_size
= tx_size
+ BUFFER_SIZE_INCREMENT
;
90 rmi_i2c
->tx_buf
= devm_kzalloc(&client
->dev
,
93 if (!rmi_i2c
->tx_buf
) {
94 rmi_i2c
->tx_buf_size
= 0;
100 rmi_i2c
->tx_buf
[0] = addr
& 0xff;
101 memcpy(rmi_i2c
->tx_buf
+ 1, buf
, len
);
103 if (RMI_I2C_PAGE(addr
) != rmi_i2c
->page
) {
104 retval
= rmi_set_page(rmi_i2c
, RMI_I2C_PAGE(addr
));
109 retval
= i2c_master_send(client
, rmi_i2c
->tx_buf
, tx_size
);
110 if (retval
== tx_size
)
112 else if (retval
>= 0)
116 rmi_dbg(RMI_DEBUG_XPORT
, &client
->dev
,
117 "write %zd bytes at %#06x: %d (%*ph)\n",
118 len
, addr
, retval
, (int)len
, buf
);
120 mutex_unlock(&rmi_i2c
->page_mutex
);
124 static int rmi_i2c_read_block(struct rmi_transport_dev
*xport
, u16 addr
,
125 void *buf
, size_t len
)
127 struct rmi_i2c_xport
*rmi_i2c
=
128 container_of(xport
, struct rmi_i2c_xport
, xport
);
129 struct i2c_client
*client
= rmi_i2c
->client
;
130 u8 addr_offset
= addr
& 0xff;
132 struct i2c_msg msgs
[] = {
134 .addr
= client
->addr
,
135 .len
= sizeof(addr_offset
),
139 .addr
= client
->addr
,
146 mutex_lock(&rmi_i2c
->page_mutex
);
148 if (RMI_I2C_PAGE(addr
) != rmi_i2c
->page
) {
149 retval
= rmi_set_page(rmi_i2c
, RMI_I2C_PAGE(addr
));
154 retval
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
155 if (retval
== ARRAY_SIZE(msgs
))
156 retval
= 0; /* success */
157 else if (retval
>= 0)
161 rmi_dbg(RMI_DEBUG_XPORT
, &client
->dev
,
162 "read %zd bytes at %#06x: %d (%*ph)\n",
163 len
, addr
, retval
, (int)len
, buf
);
165 mutex_unlock(&rmi_i2c
->page_mutex
);
169 static const struct rmi_transport_ops rmi_i2c_ops
= {
170 .write_block
= rmi_i2c_write_block
,
171 .read_block
= rmi_i2c_read_block
,
175 static const struct of_device_id rmi_i2c_of_match
[] = {
176 { .compatible
= "syna,rmi4-i2c" },
179 MODULE_DEVICE_TABLE(of
, rmi_i2c_of_match
);
182 static void rmi_i2c_regulator_bulk_disable(void *data
)
184 struct rmi_i2c_xport
*rmi_i2c
= data
;
186 regulator_bulk_disable(ARRAY_SIZE(rmi_i2c
->supplies
),
190 static void rmi_i2c_unregister_transport(void *data
)
192 struct rmi_i2c_xport
*rmi_i2c
= data
;
194 rmi_unregister_transport_device(&rmi_i2c
->xport
);
197 static int rmi_i2c_probe(struct i2c_client
*client
,
198 const struct i2c_device_id
*id
)
200 struct rmi_device_platform_data
*pdata
;
201 struct rmi_device_platform_data
*client_pdata
=
202 dev_get_platdata(&client
->dev
);
203 struct rmi_i2c_xport
*rmi_i2c
;
206 rmi_i2c
= devm_kzalloc(&client
->dev
, sizeof(struct rmi_i2c_xport
),
211 pdata
= &rmi_i2c
->xport
.pdata
;
213 if (!client
->dev
.of_node
&& client_pdata
)
214 *pdata
= *client_pdata
;
216 pdata
->irq
= client
->irq
;
218 rmi_dbg(RMI_DEBUG_XPORT
, &client
->dev
, "Probing %s.\n",
219 dev_name(&client
->dev
));
221 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
222 dev_err(&client
->dev
,
223 "adapter does not support required functionality\n");
227 rmi_i2c
->supplies
[0].supply
= "vdd";
228 rmi_i2c
->supplies
[1].supply
= "vio";
229 error
= devm_regulator_bulk_get(&client
->dev
,
230 ARRAY_SIZE(rmi_i2c
->supplies
),
235 error
= regulator_bulk_enable(ARRAY_SIZE(rmi_i2c
->supplies
),
240 error
= devm_add_action_or_reset(&client
->dev
,
241 rmi_i2c_regulator_bulk_disable
,
246 of_property_read_u32(client
->dev
.of_node
, "syna,startup-delay-ms",
247 &rmi_i2c
->startup_delay
);
249 msleep(rmi_i2c
->startup_delay
);
251 rmi_i2c
->client
= client
;
252 mutex_init(&rmi_i2c
->page_mutex
);
254 rmi_i2c
->xport
.dev
= &client
->dev
;
255 rmi_i2c
->xport
.proto_name
= "i2c";
256 rmi_i2c
->xport
.ops
= &rmi_i2c_ops
;
258 i2c_set_clientdata(client
, rmi_i2c
);
261 * Setting the page to zero will (a) make sure the PSR is in a
262 * known state, and (b) make sure we can talk to the device.
264 error
= rmi_set_page(rmi_i2c
, 0);
266 dev_err(&client
->dev
, "Failed to set page select to 0\n");
270 dev_info(&client
->dev
, "registering I2C-connected sensor\n");
272 error
= rmi_register_transport_device(&rmi_i2c
->xport
);
274 dev_err(&client
->dev
, "failed to register sensor: %d\n", error
);
278 error
= devm_add_action_or_reset(&client
->dev
,
279 rmi_i2c_unregister_transport
,
287 #ifdef CONFIG_PM_SLEEP
288 static int rmi_i2c_suspend(struct device
*dev
)
290 struct i2c_client
*client
= to_i2c_client(dev
);
291 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
294 ret
= rmi_driver_suspend(rmi_i2c
->xport
.rmi_dev
, true);
296 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
298 regulator_bulk_disable(ARRAY_SIZE(rmi_i2c
->supplies
),
304 static int rmi_i2c_resume(struct device
*dev
)
306 struct i2c_client
*client
= to_i2c_client(dev
);
307 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
310 ret
= regulator_bulk_enable(ARRAY_SIZE(rmi_i2c
->supplies
),
315 msleep(rmi_i2c
->startup_delay
);
317 ret
= rmi_driver_resume(rmi_i2c
->xport
.rmi_dev
, true);
319 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
326 static int rmi_i2c_runtime_suspend(struct device
*dev
)
328 struct i2c_client
*client
= to_i2c_client(dev
);
329 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
332 ret
= rmi_driver_suspend(rmi_i2c
->xport
.rmi_dev
, false);
334 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
336 regulator_bulk_disable(ARRAY_SIZE(rmi_i2c
->supplies
),
342 static int rmi_i2c_runtime_resume(struct device
*dev
)
344 struct i2c_client
*client
= to_i2c_client(dev
);
345 struct rmi_i2c_xport
*rmi_i2c
= i2c_get_clientdata(client
);
348 ret
= regulator_bulk_enable(ARRAY_SIZE(rmi_i2c
->supplies
),
353 msleep(rmi_i2c
->startup_delay
);
355 ret
= rmi_driver_resume(rmi_i2c
->xport
.rmi_dev
, false);
357 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
363 static const struct dev_pm_ops rmi_i2c_pm
= {
364 SET_SYSTEM_SLEEP_PM_OPS(rmi_i2c_suspend
, rmi_i2c_resume
)
365 SET_RUNTIME_PM_OPS(rmi_i2c_runtime_suspend
, rmi_i2c_runtime_resume
,
369 static const struct i2c_device_id rmi_id
[] = {
373 MODULE_DEVICE_TABLE(i2c
, rmi_id
);
375 static struct i2c_driver rmi_i2c_driver
= {
379 .of_match_table
= of_match_ptr(rmi_i2c_of_match
),
382 .probe
= rmi_i2c_probe
,
385 module_i2c_driver(rmi_i2c_driver
);
387 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>");
388 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
389 MODULE_DESCRIPTION("RMI I2C driver");
390 MODULE_LICENSE("GPL");