This source file includes following definitions.
- rmi_smb_get_version
- smb_block_write
- rmi_smb_get_command_code
- rmi_smb_write_block
- smb_block_read
- rmi_smb_read_block
- rmi_smb_clear_state
- rmi_smb_enable_smbus_mode
- rmi_smb_reset
- rmi_smb_probe
- rmi_smb_remove
- rmi_smb_suspend
- rmi_smb_runtime_suspend
- rmi_smb_resume
- rmi_smb_runtime_resume
1
2
3
4
5
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
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
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
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
77
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
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
118
119
120 memset(&new_map, 0, sizeof(new_map));
121 }
122
123
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
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
164 cur_len -= SMB_MAX_COUNT;
165 databuff += SMB_MAX_COUNT;
166 rmiaddr += SMB_MAX_COUNT;
167 }
168 exit:
169 mutex_unlock(&rmi_smb->page_mutex);
170 return retval;
171 }
172
173
174 static int smb_block_read(struct rmi_transport_dev *xport,
175 u8 commandcode, void *buf, size_t len)
176 {
177 struct rmi_smb_xport *rmi_smb =
178 container_of(xport, struct rmi_smb_xport, xport);
179 struct i2c_client *client = rmi_smb->client;
180 int retval;
181
182 retval = i2c_smbus_read_block_data(client, commandcode, buf);
183 if (retval < 0)
184 return retval;
185
186 return retval;
187 }
188
189 static int rmi_smb_read_block(struct rmi_transport_dev *xport, u16 rmiaddr,
190 void *databuff, size_t len)
191 {
192 struct rmi_smb_xport *rmi_smb =
193 container_of(xport, struct rmi_smb_xport, xport);
194 int retval;
195 u8 commandcode;
196 int cur_len = (int)len;
197
198 mutex_lock(&rmi_smb->page_mutex);
199 memset(databuff, 0, len);
200
201 while (cur_len > 0) {
202
203 int block_len = min_t(int, cur_len, SMB_MAX_COUNT);
204
205 retval = rmi_smb_get_command_code(xport, rmiaddr, block_len,
206 true, &commandcode);
207 if (retval < 0)
208 goto exit;
209
210 retval = smb_block_read(xport, commandcode,
211 databuff, block_len);
212 if (retval < 0)
213 goto exit;
214
215
216 cur_len -= SMB_MAX_COUNT;
217 databuff += SMB_MAX_COUNT;
218 rmiaddr += SMB_MAX_COUNT;
219 }
220
221 retval = 0;
222
223 exit:
224 mutex_unlock(&rmi_smb->page_mutex);
225 return retval;
226 }
227
228 static void rmi_smb_clear_state(struct rmi_smb_xport *rmi_smb)
229 {
230
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);
234 }
235
236 static int rmi_smb_enable_smbus_mode(struct rmi_smb_xport *rmi_smb)
237 {
238 int retval;
239
240
241 retval = rmi_smb_get_version(rmi_smb);
242 if (retval < 0)
243 return retval;
244
245 return 0;
246 }
247
248 static int rmi_smb_reset(struct rmi_transport_dev *xport, u16 reset_addr)
249 {
250 struct rmi_smb_xport *rmi_smb =
251 container_of(xport, struct rmi_smb_xport, xport);
252
253 rmi_smb_clear_state(rmi_smb);
254
255
256
257
258
259
260
261
262 return rmi_smb_enable_smbus_mode(rmi_smb);
263 }
264
265 static const struct rmi_transport_ops rmi_smb_ops = {
266 .write_block = rmi_smb_write_block,
267 .read_block = rmi_smb_read_block,
268 .reset = rmi_smb_reset,
269 };
270
271 static int rmi_smb_probe(struct i2c_client *client,
272 const struct i2c_device_id *id)
273 {
274 struct rmi_device_platform_data *pdata = dev_get_platdata(&client->dev);
275 struct rmi_smb_xport *rmi_smb;
276 int smbus_version;
277 int error;
278
279 if (!pdata) {
280 dev_err(&client->dev, "no platform data, aborting\n");
281 return -ENOMEM;
282 }
283
284 if (!i2c_check_functionality(client->adapter,
285 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
286 I2C_FUNC_SMBUS_HOST_NOTIFY)) {
287 dev_err(&client->dev,
288 "adapter does not support required functionality\n");
289 return -ENODEV;
290 }
291
292 if (client->irq <= 0) {
293 dev_err(&client->dev, "no IRQ provided, giving up\n");
294 return client->irq ? client->irq : -ENODEV;
295 }
296
297 rmi_smb = devm_kzalloc(&client->dev, sizeof(struct rmi_smb_xport),
298 GFP_KERNEL);
299 if (!rmi_smb)
300 return -ENOMEM;
301
302 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s\n",
303 dev_name(&client->dev));
304
305 rmi_smb->client = client;
306 mutex_init(&rmi_smb->page_mutex);
307 mutex_init(&rmi_smb->mappingtable_mutex);
308
309 rmi_smb->xport.dev = &client->dev;
310 rmi_smb->xport.pdata = *pdata;
311 rmi_smb->xport.pdata.irq = client->irq;
312 rmi_smb->xport.proto_name = "smb";
313 rmi_smb->xport.ops = &rmi_smb_ops;
314
315 smbus_version = rmi_smb_get_version(rmi_smb);
316 if (smbus_version < 0)
317 return smbus_version;
318
319 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Smbus version is %d",
320 smbus_version);
321
322 if (smbus_version != 2 && smbus_version != 3) {
323 dev_err(&client->dev, "Unrecognized SMB version %d\n",
324 smbus_version);
325 return -ENODEV;
326 }
327
328 i2c_set_clientdata(client, rmi_smb);
329
330 dev_info(&client->dev, "registering SMbus-connected sensor\n");
331
332 error = rmi_register_transport_device(&rmi_smb->xport);
333 if (error) {
334 dev_err(&client->dev, "failed to register sensor: %d\n", error);
335 return error;
336 }
337
338 return 0;
339 }
340
341 static int rmi_smb_remove(struct i2c_client *client)
342 {
343 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
344
345 rmi_unregister_transport_device(&rmi_smb->xport);
346
347 return 0;
348 }
349
350 static int __maybe_unused rmi_smb_suspend(struct device *dev)
351 {
352 struct i2c_client *client = to_i2c_client(dev);
353 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
354 int ret;
355
356 ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, true);
357 if (ret)
358 dev_warn(dev, "Failed to suspend device: %d\n", ret);
359
360 return ret;
361 }
362
363 static int __maybe_unused rmi_smb_runtime_suspend(struct device *dev)
364 {
365 struct i2c_client *client = to_i2c_client(dev);
366 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
367 int ret;
368
369 ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, false);
370 if (ret)
371 dev_warn(dev, "Failed to suspend device: %d\n", ret);
372
373 return ret;
374 }
375
376 static int __maybe_unused rmi_smb_resume(struct device *dev)
377 {
378 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
379 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
380 struct rmi_device *rmi_dev = rmi_smb->xport.rmi_dev;
381 int ret;
382
383 rmi_smb_reset(&rmi_smb->xport, 0);
384
385 rmi_reset(rmi_dev);
386
387 ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, true);
388 if (ret)
389 dev_warn(dev, "Failed to resume device: %d\n", ret);
390
391 return 0;
392 }
393
394 static int __maybe_unused rmi_smb_runtime_resume(struct device *dev)
395 {
396 struct i2c_client *client = to_i2c_client(dev);
397 struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client);
398 int ret;
399
400 ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, false);
401 if (ret)
402 dev_warn(dev, "Failed to resume device: %d\n", ret);
403
404 return 0;
405 }
406
407 static const struct dev_pm_ops rmi_smb_pm = {
408 SET_SYSTEM_SLEEP_PM_OPS(rmi_smb_suspend, rmi_smb_resume)
409 SET_RUNTIME_PM_OPS(rmi_smb_runtime_suspend, rmi_smb_runtime_resume,
410 NULL)
411 };
412
413 static const struct i2c_device_id rmi_id[] = {
414 { "rmi4_smbus", 0 },
415 { }
416 };
417 MODULE_DEVICE_TABLE(i2c, rmi_id);
418
419 static struct i2c_driver rmi_smb_driver = {
420 .driver = {
421 .name = "rmi4_smbus",
422 .pm = &rmi_smb_pm,
423 },
424 .id_table = rmi_id,
425 .probe = rmi_smb_probe,
426 .remove = rmi_smb_remove,
427 };
428
429 module_i2c_driver(rmi_smb_driver);
430
431 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
432 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>");
433 MODULE_DESCRIPTION("RMI4 SMBus driver");
434 MODULE_LICENSE("GPL");