2 * Copyright (c) 2015 - 2016 Red Hat, Inc
3 * Copyright (c) 2011, 2012 Synaptics Incorporated
4 * Copyright (c) 2011 Unixphere
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kconfig.h>
16 #include <linux/lockdep.h>
17 #include <linux/module.h>
19 #include <linux/rmi.h>
20 #include <linux/slab.h>
21 #include "rmi_driver.h"
23 #define SMB_PROTOCOL_VERSION_ADDRESS 0xfd
24 #define SMB_MAX_COUNT 32
25 #define RMI_SMB2_MAP_SIZE 8 /* 8 entry of 4 bytes each */
26 #define RMI_SMB2_MAP_FLAGS_WE 0x01
28 struct mapping_table_entry {
34 struct rmi_smb_xport {
35 struct rmi_transport_dev xport;
36 struct i2c_client *client;
38 struct mutex page_mutex;
41 struct mutex mappingtable_mutex;
42 struct mapping_table_entry mapping_table[RMI_SMB2_MAP_SIZE];
45 static int rmi_smb_get_version(struct rmi_smb_xport *rmi_smb)
47 struct i2c_client *client = rmi_smb->client;
50 /* Check if for SMBus new version device by reading version byte. */
51 retval = i2c_smbus_read_byte_data(client, SMB_PROTOCOL_VERSION_ADDRESS);
53 dev_err(&client->dev, "failed to get SMBus version number!\n");
60 /* SMB block write - wrapper over ic2_smb_write_block */
61 static int smb_block_write(struct rmi_transport_dev *xport,
62 u8 commandcode, const void *buf, size_t len)
64 struct rmi_smb_xport *rmi_smb =
65 container_of(xport, struct rmi_smb_xport, xport);
66 struct i2c_client *client = rmi_smb->client;
69 retval = i2c_smbus_write_block_data(client, commandcode, len, buf);
71 rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
72 "wrote %zd bytes at %#04x: %d (%*ph)\n",
73 len, commandcode, retval, (int)len, buf);
79 * The function to get command code for smbus operations and keeps
80 * records to the driver mapping table
82 static int rmi_smb_get_command_code(struct rmi_transport_dev *xport,
83 u16 rmiaddr, int bytecount, bool isread, u8 *commandcode)
85 struct rmi_smb_xport *rmi_smb =
86 container_of(xport, struct rmi_smb_xport, xport);
87 struct mapping_table_entry new_map;
91 mutex_lock(&rmi_smb->mappingtable_mutex);
93 for (i = 0; i < RMI_SMB2_MAP_SIZE; i++) {
94 struct mapping_table_entry *entry = &rmi_smb->mapping_table[i];
96 if (le16_to_cpu(entry->rmiaddr) == rmiaddr) {
98 if (entry->readcount == bytecount)
101 if (entry->flags & RMI_SMB2_MAP_FLAGS_WE) {
108 i = rmi_smb->table_index;
109 rmi_smb->table_index = (i + 1) % RMI_SMB2_MAP_SIZE;
111 /* constructs mapping table data entry. 4 bytes each entry */
112 memset(&new_map, 0, sizeof(new_map));
113 new_map.rmiaddr = cpu_to_le16(rmiaddr);
114 new_map.readcount = bytecount;
115 new_map.flags = !isread ? RMI_SMB2_MAP_FLAGS_WE : 0;
117 retval = smb_block_write(xport, i + 0x80, &new_map, sizeof(new_map));
120 * if not written to device mapping table
121 * clear the driver mapping table records
123 memset(&new_map, 0, sizeof(new_map));
126 /* save to the driver level mapping table */
127 rmi_smb->mapping_table[i] = new_map;
130 mutex_unlock(&rmi_smb->mappingtable_mutex);
139 static int rmi_smb_write_block(struct rmi_transport_dev *xport, u16 rmiaddr,
140 const void *databuff, size_t len)
144 struct rmi_smb_xport *rmi_smb =
145 container_of(xport, struct rmi_smb_xport, xport);
146 int cur_len = (int)len;
148 mutex_lock(&rmi_smb->page_mutex);
150 while (cur_len > 0) {
152 * break into 32 bytes chunks to write get command code
154 int block_len = min_t(int, len, SMB_MAX_COUNT);
156 retval = rmi_smb_get_command_code(xport, rmiaddr, block_len,
157 false, &commandcode);
161 retval = smb_block_write(xport, commandcode,
162 databuff, block_len);
166 /* prepare to write next block of bytes */
167 cur_len -= SMB_MAX_COUNT;
168 databuff += SMB_MAX_COUNT;
169 rmiaddr += SMB_MAX_COUNT;
172 mutex_unlock(&rmi_smb->page_mutex);
176 /* SMB block read - wrapper over ic2_smb_read_block */
177 static int smb_block_read(struct rmi_transport_dev *xport,
178 u8 commandcode, void *buf, size_t len)
180 struct rmi_smb_xport *rmi_smb =
181 container_of(xport, struct rmi_smb_xport, xport);
182 struct i2c_client *client = rmi_smb->client;
185 retval = i2c_smbus_read_block_data(client, commandcode, buf);
192 static int rmi_smb_read_block(struct rmi_transport_dev *xport, u16 rmiaddr,
193 void *databuff, size_t len)
195 struct rmi_smb_xport *rmi_smb =
196 container_of(xport, struct rmi_smb_xport, xport);
199 int cur_len = (int)len;
201 mutex_lock(&rmi_smb->page_mutex);
202 memset(databuff, 0, len);
204 while (cur_len > 0) {
205 /* break into 32 bytes chunks to write get command code */
206 int block_len = min_t(int, cur_len, SMB_MAX_COUNT);
208 retval = rmi_smb_get_command_code(xport, rmiaddr, block_len,
213 retval = smb_block_read(xport, commandcode,
214 databuff, block_len);
218 /* prepare to read next block of bytes */
219 cur_len -= SMB_MAX_COUNT;
220 databuff += SMB_MAX_COUNT;
221 rmiaddr += SMB_MAX_COUNT;
227 mutex_unlock(&rmi_smb->page_mutex);
231 static void rmi_smb_clear_state(struct rmi_smb_xport *rmi_smb)
233 /* the mapping table has been flushed, discard the current one */
234 mutex_lock(&rmi_smb->mappingtable_mutex);
235 memset(rmi_smb->mapping_table, 0, sizeof(rmi_smb->mapping_table));
236 mutex_unlock(&rmi_smb->mappingtable_mutex);
239 static int rmi_smb_enable_smbus_mode(struct rmi_smb_xport *rmi_smb)
243 /* we need to get the smbus version to activate the touchpad */
244 retval = rmi_smb_get_version(rmi_smb);
251 static int rmi_smb_reset(struct rmi_transport_dev *xport, u16 reset_addr)
253 struct rmi_smb_xport *rmi_smb =
254 container_of(xport, struct rmi_smb_xport, xport);
256 rmi_smb_clear_state(rmi_smb);
259 * we do not call the actual reset command, it has to be handled in
260 * PS/2 or there will be races between PS/2 and SMBus.
261 * PS/2 should ensure that a psmouse_reset is called before
262 * intializing the device and after it has been removed to be in a known
265 return rmi_smb_enable_smbus_mode(rmi_smb);
268 static const struct rmi_transport_ops rmi_smb_ops = {
269 .write_block = rmi_smb_write_block,
270 .read_block = rmi_smb_read_block,
271 .reset = rmi_smb_reset,
274 static int rmi_smb_probe(struct i2c_client *client,
275 const struct i2c_device_id *id)
277 struct rmi_device_platform_data *pdata = dev_get_platdata(&client->dev);
278 struct rmi_smb_xport *rmi_smb;
283 dev_err(&client->dev, "no platform data, aborting\n");
287 if (!i2c_check_functionality(client->adapter,
288 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
289 I2C_FUNC_SMBUS_HOST_NOTIFY)) {
290 dev_err(&client->dev,
291 "adapter does not support required functionality\n");
295 if (client->irq <= 0) {
296 dev_err(&client->dev, "no IRQ provided, giving up\n");
297 return client->irq ? client->irq : -ENODEV;
300 rmi_smb = devm_kzalloc(&client->dev, sizeof(struct rmi_smb_xport),
305 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s\n",
306 dev_name(&client->dev));
308 rmi_smb->client = client;
309 mutex_init(&rmi_smb->page_mutex);
310 mutex_init(&rmi_smb->mappingtable_mutex);
312 rmi_smb->xport.dev = &client->dev;
313 rmi_smb->xport.pdata = *pdata;
314 rmi_smb->xport.pdata.irq = client->irq;
315 rmi_smb->xport.proto_name = "smb";
316 rmi_smb->xport.ops = &rmi_smb_ops;
318 smbus_version = rmi_smb_get_version(rmi_smb);
319 if (smbus_version < 0)
320 return smbus_version;
322 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Smbus version is %d",
325 if (smbus_version != 2 && smbus_version != 3) {
326 dev_err(&client->dev, "Unrecognized SMB version %d\n",
331 i2c_set_clientdata(client, rmi_smb);
333 dev_info(&client->dev, "registering SMbus-connected sensor\n");
335 error = rmi_register_transport_device(&rmi_smb->xport);
337 dev_err(&client->dev, "failed to register sensor: %d\n", error);
344 static int rmi_smb_remove(struct i2c_client *client)
346 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
348 rmi_unregister_transport_device(&rmi_smb->xport);
353 static int __maybe_unused rmi_smb_suspend(struct device *dev)
355 struct i2c_client *client = to_i2c_client(dev);
356 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
359 ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, true);
361 dev_warn(dev, "Failed to suspend device: %d\n", ret);
366 static int __maybe_unused rmi_smb_runtime_suspend(struct device *dev)
368 struct i2c_client *client = to_i2c_client(dev);
369 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
372 ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, false);
374 dev_warn(dev, "Failed to suspend device: %d\n", ret);
379 static int __maybe_unused rmi_smb_resume(struct device *dev)
381 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
382 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
383 struct rmi_device *rmi_dev = rmi_smb->xport.rmi_dev;
386 rmi_smb_reset(&rmi_smb->xport, 0);
390 ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, true);
392 dev_warn(dev, "Failed to resume device: %d\n", ret);
397 static int __maybe_unused rmi_smb_runtime_resume(struct device *dev)
399 struct i2c_client *client = to_i2c_client(dev);
400 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
403 ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, false);
405 dev_warn(dev, "Failed to resume device: %d\n", ret);
410 static const struct dev_pm_ops rmi_smb_pm = {
411 SET_SYSTEM_SLEEP_PM_OPS(rmi_smb_suspend, rmi_smb_resume)
412 SET_RUNTIME_PM_OPS(rmi_smb_runtime_suspend, rmi_smb_runtime_resume,
416 static const struct i2c_device_id rmi_id[] = {
420 MODULE_DEVICE_TABLE(i2c, rmi_id);
422 static struct i2c_driver rmi_smb_driver = {
424 .name = "rmi4_smbus",
428 .probe = rmi_smb_probe,
429 .remove = rmi_smb_remove,
432 module_i2c_driver(rmi_smb_driver);
434 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
435 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>");
436 MODULE_DESCRIPTION("RMI4 SMBus driver");
437 MODULE_LICENSE("GPL");