mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_i2c.c
bloba96a326b53bd363bad610712c133a669a0f090d6
1 /*
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.
8 */
10 #include <linux/i2c.h>
11 #include <linux/rmi.h>
12 #include <linux/irq.h>
13 #include <linux/of.h>
14 #include "rmi_driver.h"
16 #define BUFFER_SIZE_INCREMENT 32
18 /**
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;
34 int page;
36 int irq;
38 u8 *tx_buf;
39 size_t tx_buf_size;
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};
63 int retval;
65 retval = i2c_master_send(client, txbuf, sizeof(txbuf));
66 if (retval != sizeof(txbuf)) {
67 dev_err(&client->dev,
68 "%s: set page failed: %d.", __func__, retval);
69 return (retval < 0) ? retval : -EIO;
72 rmi_i2c->page = page;
73 return 0;
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;
83 int retval;
85 mutex_lock(&rmi_i2c->page_mutex);
87 if (!rmi_i2c->tx_buf || rmi_i2c->tx_buf_size < tx_size) {
88 if (rmi_i2c->tx_buf)
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,
92 rmi_i2c->tx_buf_size,
93 GFP_KERNEL);
94 if (!rmi_i2c->tx_buf) {
95 rmi_i2c->tx_buf_size = 0;
96 retval = -ENOMEM;
97 goto exit;
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));
106 if (retval)
107 goto exit;
110 retval = i2c_master_send(client, rmi_i2c->tx_buf, tx_size);
111 if (retval == tx_size)
112 retval = 0;
113 else if (retval >= 0)
114 retval = -EIO;
116 exit:
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);
122 return retval;
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;
132 int retval;
133 struct i2c_msg msgs[] = {
135 .addr = client->addr,
136 .len = sizeof(addr_offset),
137 .buf = &addr_offset,
140 .addr = client->addr,
141 .flags = I2C_M_RD,
142 .len = len,
143 .buf = buf,
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));
151 if (retval)
152 goto exit;
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)
159 retval = -EIO;
161 exit:
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);
167 return retval;
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;
179 int ret;
181 ret = rmi_process_interrupt_requests(rmi_dev);
182 if (ret)
183 rmi_dbg(RMI_DEBUG_XPORT, &rmi_dev->dev,
184 "Failed to process interrupt request: %d\n", ret);
186 return IRQ_HANDLED;
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));
193 int ret;
195 if (!irq_flags)
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,
200 rmi_i2c);
201 if (ret < 0) {
202 dev_warn(&client->dev, "Failed to register interrupt %d\n",
203 rmi_i2c->irq);
205 return ret;
208 return 0;
211 #ifdef CONFIG_OF
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);
217 #endif
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;
226 int retval;
228 rmi_i2c = devm_kzalloc(&client->dev, sizeof(struct rmi_i2c_xport),
229 GFP_KERNEL);
230 if (!rmi_i2c)
231 return -ENOMEM;
233 pdata = &rmi_i2c->xport.pdata;
235 if (!client->dev.of_node && client_pdata)
236 *pdata = *client_pdata;
238 if (client->irq > 0)
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");
246 return -ENODEV;
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);
263 if (retval) {
264 dev_err(&client->dev, "Failed to set page select to 0.\n");
265 return retval;
268 retval = rmi_register_transport_device(&rmi_i2c->xport);
269 if (retval) {
270 dev_err(&client->dev, "Failed to register transport driver at 0x%.2X.\n",
271 client->addr);
272 return retval;
275 retval = rmi_i2c_init_irq(client);
276 if (retval < 0)
277 return retval;
279 dev_info(&client->dev, "registered rmi i2c driver at %#04x.\n",
280 client->addr);
281 return 0;
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);
290 return 0;
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);
298 int ret;
300 ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev);
301 if (ret)
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);
307 if (!ret)
308 dev_warn(dev, "Failed to enable irq for wake: %d\n",
309 ret);
311 return ret;
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);
318 int ret;
320 enable_irq(rmi_i2c->irq);
321 if (device_may_wakeup(&client->dev)) {
322 ret = disable_irq_wake(rmi_i2c->irq);
323 if (!ret)
324 dev_warn(dev, "Failed to disable irq for wake: %d\n",
325 ret);
328 ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev);
329 if (ret)
330 dev_warn(dev, "Failed to resume device: %d\n", ret);
332 return ret;
334 #endif
336 #ifdef CONFIG_PM
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);
341 int ret;
343 ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev);
344 if (ret)
345 dev_warn(dev, "Failed to resume device: %d\n", ret);
347 disable_irq(rmi_i2c->irq);
349 return 0;
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);
356 int ret;
358 enable_irq(rmi_i2c->irq);
360 ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev);
361 if (ret)
362 dev_warn(dev, "Failed to resume device: %d\n", ret);
364 return 0;
366 #endif
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,
371 NULL)
374 static const struct i2c_device_id rmi_id[] = {
375 { "rmi4_i2c", 0 },
378 MODULE_DEVICE_TABLE(i2c, rmi_id);
380 static struct i2c_driver rmi_i2c_driver = {
381 .driver = {
382 .name = "rmi4_i2c",
383 .pm = &rmi_i2c_pm,
384 .of_match_table = of_match_ptr(rmi_i2c_of_match),
386 .id_table = rmi_id,
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);