treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_i2c.c
bloba95c2c9bcab4ca8ebe142f2e2a0b373463af9504
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2011-2016 Synaptics Incorporated
4 * Copyright (c) 2011 Unixphere
5 */
7 #include <linux/i2c.h>
8 #include <linux/rmi.h>
9 #include <linux/of.h>
10 #include <linux/delay.h>
11 #include <linux/regulator/consumer.h>
12 #include "rmi_driver.h"
14 #define BUFFER_SIZE_INCREMENT 32
16 /**
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;
32 int page;
34 u8 *tx_buf;
35 size_t tx_buf_size;
37 struct regulator_bulk_data supplies[2];
38 u32 startup_delay;
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};
62 int retval;
64 retval = i2c_master_send(client, txbuf, sizeof(txbuf));
65 if (retval != sizeof(txbuf)) {
66 dev_err(&client->dev,
67 "%s: set page failed: %d.", __func__, retval);
68 return (retval < 0) ? retval : -EIO;
71 rmi_i2c->page = page;
72 return 0;
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;
82 int retval;
84 mutex_lock(&rmi_i2c->page_mutex);
86 if (!rmi_i2c->tx_buf || rmi_i2c->tx_buf_size < tx_size) {
87 if (rmi_i2c->tx_buf)
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,
91 rmi_i2c->tx_buf_size,
92 GFP_KERNEL);
93 if (!rmi_i2c->tx_buf) {
94 rmi_i2c->tx_buf_size = 0;
95 retval = -ENOMEM;
96 goto exit;
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));
105 if (retval)
106 goto exit;
109 retval = i2c_master_send(client, rmi_i2c->tx_buf, tx_size);
110 if (retval == tx_size)
111 retval = 0;
112 else if (retval >= 0)
113 retval = -EIO;
115 exit:
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);
121 return retval;
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;
131 int retval;
132 struct i2c_msg msgs[] = {
134 .addr = client->addr,
135 .len = sizeof(addr_offset),
136 .buf = &addr_offset,
139 .addr = client->addr,
140 .flags = I2C_M_RD,
141 .len = len,
142 .buf = buf,
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));
150 if (retval)
151 goto exit;
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)
158 retval = -EIO;
160 exit:
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);
166 return retval;
169 static const struct rmi_transport_ops rmi_i2c_ops = {
170 .write_block = rmi_i2c_write_block,
171 .read_block = rmi_i2c_read_block,
174 #ifdef CONFIG_OF
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);
180 #endif
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),
187 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;
204 int error;
206 rmi_i2c = devm_kzalloc(&client->dev, sizeof(struct rmi_i2c_xport),
207 GFP_KERNEL);
208 if (!rmi_i2c)
209 return -ENOMEM;
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");
224 return -ENODEV;
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),
231 rmi_i2c->supplies);
232 if (error < 0)
233 return error;
235 error = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
236 rmi_i2c->supplies);
237 if (error < 0)
238 return error;
240 error = devm_add_action_or_reset(&client->dev,
241 rmi_i2c_regulator_bulk_disable,
242 rmi_i2c);
243 if (error)
244 return error;
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);
265 if (error) {
266 dev_err(&client->dev, "Failed to set page select to 0\n");
267 return error;
270 dev_info(&client->dev, "registering I2C-connected sensor\n");
272 error = rmi_register_transport_device(&rmi_i2c->xport);
273 if (error) {
274 dev_err(&client->dev, "failed to register sensor: %d\n", error);
275 return error;
278 error = devm_add_action_or_reset(&client->dev,
279 rmi_i2c_unregister_transport,
280 rmi_i2c);
281 if (error)
282 return error;
284 return 0;
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);
292 int ret;
294 ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev, true);
295 if (ret)
296 dev_warn(dev, "Failed to resume device: %d\n", ret);
298 regulator_bulk_disable(ARRAY_SIZE(rmi_i2c->supplies),
299 rmi_i2c->supplies);
301 return ret;
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);
308 int ret;
310 ret = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
311 rmi_i2c->supplies);
312 if (ret)
313 return ret;
315 msleep(rmi_i2c->startup_delay);
317 ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev, true);
318 if (ret)
319 dev_warn(dev, "Failed to resume device: %d\n", ret);
321 return ret;
323 #endif
325 #ifdef CONFIG_PM
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);
330 int ret;
332 ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev, false);
333 if (ret)
334 dev_warn(dev, "Failed to resume device: %d\n", ret);
336 regulator_bulk_disable(ARRAY_SIZE(rmi_i2c->supplies),
337 rmi_i2c->supplies);
339 return 0;
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);
346 int ret;
348 ret = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
349 rmi_i2c->supplies);
350 if (ret)
351 return ret;
353 msleep(rmi_i2c->startup_delay);
355 ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev, false);
356 if (ret)
357 dev_warn(dev, "Failed to resume device: %d\n", ret);
359 return 0;
361 #endif
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,
366 NULL)
369 static const struct i2c_device_id rmi_id[] = {
370 { "rmi4_i2c", 0 },
373 MODULE_DEVICE_TABLE(i2c, rmi_id);
375 static struct i2c_driver rmi_i2c_driver = {
376 .driver = {
377 .name = "rmi4_i2c",
378 .pm = &rmi_i2c_pm,
379 .of_match_table = of_match_ptr(rmi_i2c_of_match),
381 .id_table = rmi_id,
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");