1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 - 2016 Red Hat, Inc
4 * Copyright (c) 2011, 2012 Synaptics Incorporated
5 * Copyright (c) 2011 Unixphere
8 #include <linux/kernel.h>
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kconfig.h>
13 #include <linux/lockdep.h>
14 #include <linux/module.h>
16 #include <linux/rmi.h>
17 #include <linux/slab.h>
18 #include "rmi_driver.h"
20 #define SMB_PROTOCOL_VERSION_ADDRESS 0xfd
21 #define SMB_MAX_COUNT 32
22 #define RMI_SMB2_MAP_SIZE 8 /* 8 entry of 4 bytes each */
23 #define RMI_SMB2_MAP_FLAGS_WE 0x01
25 struct mapping_table_entry {
31 struct rmi_smb_xport {
32 struct rmi_transport_dev xport;
33 struct i2c_client *client;
35 struct mutex page_mutex;
38 struct mutex mappingtable_mutex;
39 struct mapping_table_entry mapping_table[RMI_SMB2_MAP_SIZE];
42 static int rmi_smb_get_version(struct rmi_smb_xport *rmi_smb)
44 struct i2c_client *client = rmi_smb->client;
47 /* Check if for SMBus new version device by reading version byte. */
48 retval = i2c_smbus_read_byte_data(client, SMB_PROTOCOL_VERSION_ADDRESS);
50 dev_err(&client->dev, "failed to get SMBus version number!\n");
57 /* SMB block write - wrapper over ic2_smb_write_block */
58 static int smb_block_write(struct rmi_transport_dev *xport,
59 u8 commandcode, const void *buf, size_t len)
61 struct rmi_smb_xport *rmi_smb =
62 container_of(xport, struct rmi_smb_xport, xport);
63 struct i2c_client *client = rmi_smb->client;
66 retval = i2c_smbus_write_block_data(client, commandcode, len, buf);
68 rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
69 "wrote %zd bytes at %#04x: %d (%*ph)\n",
70 len, commandcode, retval, (int)len, buf);
76 * The function to get command code for smbus operations and keeps
77 * records to the driver mapping table
79 static int rmi_smb_get_command_code(struct rmi_transport_dev *xport,
80 u16 rmiaddr, int bytecount, bool isread, u8 *commandcode)
82 struct rmi_smb_xport *rmi_smb =
83 container_of(xport, struct rmi_smb_xport, xport);
84 struct mapping_table_entry new_map;
88 mutex_lock(&rmi_smb->mappingtable_mutex);
90 for (i = 0; i < RMI_SMB2_MAP_SIZE; i++) {
91 struct mapping_table_entry *entry = &rmi_smb->mapping_table[i];
93 if (le16_to_cpu(entry->rmiaddr) == rmiaddr) {
95 if (entry->readcount == bytecount)
98 if (entry->flags & RMI_SMB2_MAP_FLAGS_WE) {
105 i = rmi_smb->table_index;
106 rmi_smb->table_index = (i + 1) % RMI_SMB2_MAP_SIZE;
108 /* constructs mapping table data entry. 4 bytes each entry */
109 memset(&new_map, 0, sizeof(new_map));
110 new_map.rmiaddr = cpu_to_le16(rmiaddr);
111 new_map.readcount = bytecount;
112 new_map.flags = !isread ? RMI_SMB2_MAP_FLAGS_WE : 0;
114 retval = smb_block_write(xport, i + 0x80, &new_map, sizeof(new_map));
117 * if not written to device mapping table
118 * clear the driver mapping table records
120 memset(&new_map, 0, sizeof(new_map));
123 /* save to the driver level mapping table */
124 rmi_smb->mapping_table[i] = new_map;
127 mutex_unlock(&rmi_smb->mappingtable_mutex);
136 static int rmi_smb_write_block(struct rmi_transport_dev *xport, u16 rmiaddr,
137 const void *databuff, size_t len)
141 struct rmi_smb_xport *rmi_smb =
142 container_of(xport, struct rmi_smb_xport, xport);
143 int cur_len = (int)len;
145 mutex_lock(&rmi_smb->page_mutex);
147 while (cur_len > 0) {
149 * break into 32 bytes chunks to write get command code
151 int block_len = min_t(int, len, SMB_MAX_COUNT);
153 retval = rmi_smb_get_command_code(xport, rmiaddr, block_len,
154 false, &commandcode);
158 retval = smb_block_write(xport, commandcode,
159 databuff, block_len);
163 /* prepare to write next block of bytes */
164 cur_len -= SMB_MAX_COUNT;
165 databuff += SMB_MAX_COUNT;
166 rmiaddr += SMB_MAX_COUNT;
169 mutex_unlock(&rmi_smb->page_mutex);
173 /* SMB block read - wrapper over ic2_smb_read_block */
174 static int smb_block_read(struct rmi_transport_dev *xport,
175 u8 commandcode, void *buf, size_t len)
177 struct rmi_smb_xport *rmi_smb =
178 container_of(xport, struct rmi_smb_xport, xport);
179 struct i2c_client *client = rmi_smb->client;
182 retval = i2c_smbus_read_block_data(client, commandcode, buf);
189 static int rmi_smb_read_block(struct rmi_transport_dev *xport, u16 rmiaddr,
190 void *databuff, size_t len)
192 struct rmi_smb_xport *rmi_smb =
193 container_of(xport, struct rmi_smb_xport, xport);
196 int cur_len = (int)len;
198 mutex_lock(&rmi_smb->page_mutex);
199 memset(databuff, 0, len);
201 while (cur_len > 0) {
202 /* break into 32 bytes chunks to write get command code */
203 int block_len = min_t(int, cur_len, SMB_MAX_COUNT);
205 retval = rmi_smb_get_command_code(xport, rmiaddr, block_len,
210 retval = smb_block_read(xport, commandcode,
211 databuff, block_len);
215 /* prepare to read next block of bytes */
216 cur_len -= SMB_MAX_COUNT;
217 databuff += SMB_MAX_COUNT;
218 rmiaddr += SMB_MAX_COUNT;
224 mutex_unlock(&rmi_smb->page_mutex);
228 static void rmi_smb_clear_state(struct rmi_smb_xport *rmi_smb)
230 /* the mapping table has been flushed, discard the current one */
231 mutex_lock(&rmi_smb->mappingtable_mutex);
232 memset(rmi_smb->mapping_table, 0, sizeof(rmi_smb->mapping_table));
233 mutex_unlock(&rmi_smb->mappingtable_mutex);
236 static int rmi_smb_enable_smbus_mode(struct rmi_smb_xport *rmi_smb)
238 struct i2c_client *client = rmi_smb->client;
242 * psmouse driver resets the controller, we only need to wait
243 * to give the firmware chance to fully reinitialize.
245 if (rmi_smb->xport.pdata.reset_delay_ms)
246 msleep(rmi_smb->xport.pdata.reset_delay_ms);
248 /* we need to get the smbus version to activate the touchpad */
249 smbus_version = rmi_smb_get_version(rmi_smb);
250 if (smbus_version < 0)
251 return smbus_version;
253 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Smbus version is %d",
256 if (smbus_version != 2 && smbus_version != 3) {
257 dev_err(&client->dev, "Unrecognized SMB version %d\n",
265 static int rmi_smb_reset(struct rmi_transport_dev *xport, u16 reset_addr)
267 struct rmi_smb_xport *rmi_smb =
268 container_of(xport, struct rmi_smb_xport, xport);
270 rmi_smb_clear_state(rmi_smb);
273 * We do not call the actual reset command, it has to be handled in
274 * PS/2 or there will be races between PS/2 and SMBus. PS/2 should
275 * ensure that a psmouse_reset is called before initializing the
276 * device and after it has been removed to be in a known state.
278 return rmi_smb_enable_smbus_mode(rmi_smb);
281 static const struct rmi_transport_ops rmi_smb_ops = {
282 .write_block = rmi_smb_write_block,
283 .read_block = rmi_smb_read_block,
284 .reset = rmi_smb_reset,
287 static int rmi_smb_probe(struct i2c_client *client,
288 const struct i2c_device_id *id)
290 struct rmi_device_platform_data *pdata = dev_get_platdata(&client->dev);
291 struct rmi_smb_xport *rmi_smb;
295 dev_err(&client->dev, "no platform data, aborting\n");
299 if (!i2c_check_functionality(client->adapter,
300 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
301 I2C_FUNC_SMBUS_HOST_NOTIFY)) {
302 dev_err(&client->dev,
303 "adapter does not support required functionality\n");
307 if (client->irq <= 0) {
308 dev_err(&client->dev, "no IRQ provided, giving up\n");
309 return client->irq ? client->irq : -ENODEV;
312 rmi_smb = devm_kzalloc(&client->dev, sizeof(struct rmi_smb_xport),
317 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s\n",
318 dev_name(&client->dev));
320 rmi_smb->client = client;
321 mutex_init(&rmi_smb->page_mutex);
322 mutex_init(&rmi_smb->mappingtable_mutex);
324 rmi_smb->xport.dev = &client->dev;
325 rmi_smb->xport.pdata = *pdata;
326 rmi_smb->xport.pdata.irq = client->irq;
327 rmi_smb->xport.proto_name = "smb";
328 rmi_smb->xport.ops = &rmi_smb_ops;
330 error = rmi_smb_enable_smbus_mode(rmi_smb);
334 i2c_set_clientdata(client, rmi_smb);
336 dev_info(&client->dev, "registering SMbus-connected sensor\n");
338 error = rmi_register_transport_device(&rmi_smb->xport);
340 dev_err(&client->dev, "failed to register sensor: %d\n", error);
347 static int rmi_smb_remove(struct i2c_client *client)
349 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
351 rmi_unregister_transport_device(&rmi_smb->xport);
356 static int __maybe_unused rmi_smb_suspend(struct device *dev)
358 struct i2c_client *client = to_i2c_client(dev);
359 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
362 ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, true);
364 dev_warn(dev, "Failed to suspend device: %d\n", ret);
369 static int __maybe_unused rmi_smb_runtime_suspend(struct device *dev)
371 struct i2c_client *client = to_i2c_client(dev);
372 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
375 ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, false);
377 dev_warn(dev, "Failed to suspend device: %d\n", ret);
382 static int __maybe_unused rmi_smb_resume(struct device *dev)
384 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
385 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
386 struct rmi_device *rmi_dev = rmi_smb->xport.rmi_dev;
389 rmi_smb_reset(&rmi_smb->xport, 0);
393 ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, true);
395 dev_warn(dev, "Failed to resume device: %d\n", ret);
400 static int __maybe_unused rmi_smb_runtime_resume(struct device *dev)
402 struct i2c_client *client = to_i2c_client(dev);
403 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
406 ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, false);
408 dev_warn(dev, "Failed to resume device: %d\n", ret);
413 static const struct dev_pm_ops rmi_smb_pm = {
414 SET_SYSTEM_SLEEP_PM_OPS(rmi_smb_suspend, rmi_smb_resume)
415 SET_RUNTIME_PM_OPS(rmi_smb_runtime_suspend, rmi_smb_runtime_resume,
419 static const struct i2c_device_id rmi_id[] = {
423 MODULE_DEVICE_TABLE(i2c, rmi_id);
425 static struct i2c_driver rmi_smb_driver = {
427 .name = "rmi4_smbus",
431 .probe = rmi_smb_probe,
432 .remove = rmi_smb_remove,
435 module_i2c_driver(rmi_smb_driver);
437 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
438 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>");
439 MODULE_DESCRIPTION("RMI4 SMBus driver");
440 MODULE_LICENSE("GPL");