xref: /openbmc/linux/drivers/input/rmi4/rmi_smbus.c (revision 9a6b55ac)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 - 2016 Red Hat, Inc
4  * Copyright (c) 2011, 2012 Synaptics Incorporated
5  * Copyright (c) 2011 Unixphere
6  */
7 
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>
15 #include <linux/pm.h>
16 #include <linux/rmi.h>
17 #include <linux/slab.h>
18 #include "rmi_driver.h"
19 
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
24 
25 struct mapping_table_entry {
26 	__le16 rmiaddr;
27 	u8 readcount;
28 	u8 flags;
29 };
30 
31 struct rmi_smb_xport {
32 	struct rmi_transport_dev xport;
33 	struct i2c_client *client;
34 
35 	struct mutex page_mutex;
36 	int page;
37 	u8 table_index;
38 	struct mutex mappingtable_mutex;
39 	struct mapping_table_entry mapping_table[RMI_SMB2_MAP_SIZE];
40 };
41 
42 static int rmi_smb_get_version(struct rmi_smb_xport *rmi_smb)
43 {
44 	struct i2c_client *client = rmi_smb->client;
45 	int retval;
46 
47 	/* Check if for SMBus new version device by reading version byte. */
48 	retval = i2c_smbus_read_byte_data(client, SMB_PROTOCOL_VERSION_ADDRESS);
49 	if (retval < 0) {
50 		dev_err(&client->dev, "failed to get SMBus version number!\n");
51 		return retval;
52 	}
53 
54 	return retval + 1;
55 }
56 
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)
60 {
61 	struct rmi_smb_xport *rmi_smb =
62 		container_of(xport, struct rmi_smb_xport, xport);
63 	struct i2c_client *client = rmi_smb->client;
64 	int retval;
65 
66 	retval = i2c_smbus_write_block_data(client, commandcode, len, buf);
67 
68 	rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
69 		"wrote %zd bytes at %#04x: %d (%*ph)\n",
70 		len, commandcode, retval, (int)len, buf);
71 
72 	return retval;
73 }
74 
75 /*
76  * The function to get command code for smbus operations and keeps
77  * records to the driver mapping table
78  */
79 static int rmi_smb_get_command_code(struct rmi_transport_dev *xport,
80 		u16 rmiaddr, int bytecount, bool isread, u8 *commandcode)
81 {
82 	struct rmi_smb_xport *rmi_smb =
83 		container_of(xport, struct rmi_smb_xport, xport);
84 	struct mapping_table_entry new_map;
85 	int i;
86 	int retval = 0;
87 
88 	mutex_lock(&rmi_smb->mappingtable_mutex);
89 
90 	for (i = 0; i < RMI_SMB2_MAP_SIZE; i++) {
91 		struct mapping_table_entry *entry = &rmi_smb->mapping_table[i];
92 
93 		if (le16_to_cpu(entry->rmiaddr) == rmiaddr) {
94 			if (isread) {
95 				if (entry->readcount == bytecount)
96 					goto exit;
97 			} else {
98 				if (entry->flags & RMI_SMB2_MAP_FLAGS_WE) {
99 					goto exit;
100 				}
101 			}
102 		}
103 	}
104 
105 	i = rmi_smb->table_index;
106 	rmi_smb->table_index = (i + 1) % RMI_SMB2_MAP_SIZE;
107 
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;
113 
114 	retval = smb_block_write(xport, i + 0x80, &new_map, sizeof(new_map));
115 	if (retval < 0) {
116 		/*
117 		 * if not written to device mapping table
118 		 * clear the driver mapping table records
119 		 */
120 		memset(&new_map, 0, sizeof(new_map));
121 	}
122 
123 	/* save to the driver level mapping table */
124 	rmi_smb->mapping_table[i] = new_map;
125 
126 exit:
127 	mutex_unlock(&rmi_smb->mappingtable_mutex);
128 
129 	if (retval < 0)
130 		return retval;
131 
132 	*commandcode = i;
133 	return 0;
134 }
135 
136 static int rmi_smb_write_block(struct rmi_transport_dev *xport, u16 rmiaddr,
137 				const void *databuff, size_t len)
138 {
139 	int retval = 0;
140 	u8 commandcode;
141 	struct rmi_smb_xport *rmi_smb =
142 		container_of(xport, struct rmi_smb_xport, xport);
143 	int cur_len = (int)len;
144 
145 	mutex_lock(&rmi_smb->page_mutex);
146 
147 	while (cur_len > 0) {
148 		/*
149 		 * break into 32 bytes chunks to write get command code
150 		 */
151 		int block_len = min_t(int, len, SMB_MAX_COUNT);
152 
153 		retval = rmi_smb_get_command_code(xport, rmiaddr, block_len,
154 						  false, &commandcode);
155 		if (retval < 0)
156 			goto exit;
157 
158 		retval = smb_block_write(xport, commandcode,
159 					 databuff, block_len);
160 		if (retval < 0)
161 			goto exit;
162 
163 		/* prepare to write next block of bytes */
164 		cur_len -= SMB_MAX_COUNT;
165 		databuff += SMB_MAX_COUNT;
166 	}
167 exit:
168 	mutex_unlock(&rmi_smb->page_mutex);
169 	return retval;
170 }
171 
172 /* SMB block read - wrapper over ic2_smb_read_block */
173 static int smb_block_read(struct rmi_transport_dev *xport,
174 			     u8 commandcode, void *buf, size_t len)
175 {
176 	struct rmi_smb_xport *rmi_smb =
177 		container_of(xport, struct rmi_smb_xport, xport);
178 	struct i2c_client *client = rmi_smb->client;
179 	int retval;
180 
181 	retval = i2c_smbus_read_block_data(client, commandcode, buf);
182 	if (retval < 0)
183 		return retval;
184 
185 	return retval;
186 }
187 
188 static int rmi_smb_read_block(struct rmi_transport_dev *xport, u16 rmiaddr,
189 			      void *databuff, size_t len)
190 {
191 	struct rmi_smb_xport *rmi_smb =
192 		container_of(xport, struct rmi_smb_xport, xport);
193 	int retval;
194 	u8 commandcode;
195 	int cur_len = (int)len;
196 
197 	mutex_lock(&rmi_smb->page_mutex);
198 	memset(databuff, 0, len);
199 
200 	while (cur_len > 0) {
201 		/* break into 32 bytes chunks to write get command code */
202 		int block_len =  min_t(int, cur_len, SMB_MAX_COUNT);
203 
204 		retval = rmi_smb_get_command_code(xport, rmiaddr, block_len,
205 						  true, &commandcode);
206 		if (retval < 0)
207 			goto exit;
208 
209 		retval = smb_block_read(xport, commandcode,
210 					databuff, block_len);
211 		if (retval < 0)
212 			goto exit;
213 
214 		/* prepare to read next block of bytes */
215 		cur_len -= SMB_MAX_COUNT;
216 		databuff += SMB_MAX_COUNT;
217 	}
218 
219 	retval = 0;
220 
221 exit:
222 	mutex_unlock(&rmi_smb->page_mutex);
223 	return retval;
224 }
225 
226 static void rmi_smb_clear_state(struct rmi_smb_xport *rmi_smb)
227 {
228 	/* the mapping table has been flushed, discard the current one */
229 	mutex_lock(&rmi_smb->mappingtable_mutex);
230 	memset(rmi_smb->mapping_table, 0, sizeof(rmi_smb->mapping_table));
231 	mutex_unlock(&rmi_smb->mappingtable_mutex);
232 }
233 
234 static int rmi_smb_enable_smbus_mode(struct rmi_smb_xport *rmi_smb)
235 {
236 	int retval;
237 
238 	/* we need to get the smbus version to activate the touchpad */
239 	retval = rmi_smb_get_version(rmi_smb);
240 	if (retval < 0)
241 		return retval;
242 
243 	return 0;
244 }
245 
246 static int rmi_smb_reset(struct rmi_transport_dev *xport, u16 reset_addr)
247 {
248 	struct rmi_smb_xport *rmi_smb =
249 		container_of(xport, struct rmi_smb_xport, xport);
250 
251 	rmi_smb_clear_state(rmi_smb);
252 
253 	/*
254 	 * we do not call the actual reset command, it has to be handled in
255 	 * PS/2 or there will be races between PS/2 and SMBus.
256 	 * PS/2 should ensure that a psmouse_reset is called before
257 	 * intializing the device and after it has been removed to be in a known
258 	 * state.
259 	 */
260 	return rmi_smb_enable_smbus_mode(rmi_smb);
261 }
262 
263 static const struct rmi_transport_ops rmi_smb_ops = {
264 	.write_block	= rmi_smb_write_block,
265 	.read_block	= rmi_smb_read_block,
266 	.reset		= rmi_smb_reset,
267 };
268 
269 static int rmi_smb_probe(struct i2c_client *client,
270 			 const struct i2c_device_id *id)
271 {
272 	struct rmi_device_platform_data *pdata = dev_get_platdata(&client->dev);
273 	struct rmi_smb_xport *rmi_smb;
274 	int smbus_version;
275 	int error;
276 
277 	if (!pdata) {
278 		dev_err(&client->dev, "no platform data, aborting\n");
279 		return -ENOMEM;
280 	}
281 
282 	if (!i2c_check_functionality(client->adapter,
283 				     I2C_FUNC_SMBUS_READ_BLOCK_DATA |
284 				     I2C_FUNC_SMBUS_HOST_NOTIFY)) {
285 		dev_err(&client->dev,
286 			"adapter does not support required functionality\n");
287 		return -ENODEV;
288 	}
289 
290 	if (client->irq <= 0) {
291 		dev_err(&client->dev, "no IRQ provided, giving up\n");
292 		return client->irq ? client->irq : -ENODEV;
293 	}
294 
295 	rmi_smb = devm_kzalloc(&client->dev, sizeof(struct rmi_smb_xport),
296 				GFP_KERNEL);
297 	if (!rmi_smb)
298 		return -ENOMEM;
299 
300 	rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s\n",
301 		dev_name(&client->dev));
302 
303 	rmi_smb->client = client;
304 	mutex_init(&rmi_smb->page_mutex);
305 	mutex_init(&rmi_smb->mappingtable_mutex);
306 
307 	rmi_smb->xport.dev = &client->dev;
308 	rmi_smb->xport.pdata = *pdata;
309 	rmi_smb->xport.pdata.irq = client->irq;
310 	rmi_smb->xport.proto_name = "smb";
311 	rmi_smb->xport.ops = &rmi_smb_ops;
312 
313 	smbus_version = rmi_smb_get_version(rmi_smb);
314 	if (smbus_version < 0)
315 		return smbus_version;
316 
317 	rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Smbus version is %d",
318 		smbus_version);
319 
320 	if (smbus_version != 2 && smbus_version != 3) {
321 		dev_err(&client->dev, "Unrecognized SMB version %d\n",
322 				smbus_version);
323 		return -ENODEV;
324 	}
325 
326 	i2c_set_clientdata(client, rmi_smb);
327 
328 	dev_info(&client->dev, "registering SMbus-connected sensor\n");
329 
330 	error = rmi_register_transport_device(&rmi_smb->xport);
331 	if (error) {
332 		dev_err(&client->dev, "failed to register sensor: %d\n", error);
333 		return error;
334 	}
335 
336 	return 0;
337 }
338 
339 static int rmi_smb_remove(struct i2c_client *client)
340 {
341 	struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
342 
343 	rmi_unregister_transport_device(&rmi_smb->xport);
344 
345 	return 0;
346 }
347 
348 static int __maybe_unused rmi_smb_suspend(struct device *dev)
349 {
350 	struct i2c_client *client = to_i2c_client(dev);
351 	struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
352 	int ret;
353 
354 	ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, true);
355 	if (ret)
356 		dev_warn(dev, "Failed to suspend device: %d\n", ret);
357 
358 	return ret;
359 }
360 
361 static int __maybe_unused rmi_smb_runtime_suspend(struct device *dev)
362 {
363 	struct i2c_client *client = to_i2c_client(dev);
364 	struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
365 	int ret;
366 
367 	ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, false);
368 	if (ret)
369 		dev_warn(dev, "Failed to suspend device: %d\n", ret);
370 
371 	return ret;
372 }
373 
374 static int __maybe_unused rmi_smb_resume(struct device *dev)
375 {
376 	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
377 	struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
378 	struct rmi_device *rmi_dev = rmi_smb->xport.rmi_dev;
379 	int ret;
380 
381 	rmi_smb_reset(&rmi_smb->xport, 0);
382 
383 	rmi_reset(rmi_dev);
384 
385 	ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, true);
386 	if (ret)
387 		dev_warn(dev, "Failed to resume device: %d\n", ret);
388 
389 	return 0;
390 }
391 
392 static int __maybe_unused rmi_smb_runtime_resume(struct device *dev)
393 {
394 	struct i2c_client *client = to_i2c_client(dev);
395 	struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
396 	int ret;
397 
398 	ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, false);
399 	if (ret)
400 		dev_warn(dev, "Failed to resume device: %d\n", ret);
401 
402 	return 0;
403 }
404 
405 static const struct dev_pm_ops rmi_smb_pm = {
406 	SET_SYSTEM_SLEEP_PM_OPS(rmi_smb_suspend, rmi_smb_resume)
407 	SET_RUNTIME_PM_OPS(rmi_smb_runtime_suspend, rmi_smb_runtime_resume,
408 			   NULL)
409 };
410 
411 static const struct i2c_device_id rmi_id[] = {
412 	{ "rmi4_smbus", 0 },
413 	{ }
414 };
415 MODULE_DEVICE_TABLE(i2c, rmi_id);
416 
417 static struct i2c_driver rmi_smb_driver = {
418 	.driver = {
419 		.name	= "rmi4_smbus",
420 		.pm	= &rmi_smb_pm,
421 	},
422 	.id_table	= rmi_id,
423 	.probe		= rmi_smb_probe,
424 	.remove		= rmi_smb_remove,
425 };
426 
427 module_i2c_driver(rmi_smb_driver);
428 
429 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
430 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>");
431 MODULE_DESCRIPTION("RMI4 SMBus driver");
432 MODULE_LICENSE("GPL");
433