xref: /openbmc/linux/drivers/input/rmi4/rmi_i2c.c (revision f7c35abe)
1 /*
2  * Copyright (c) 2011-2016 Synaptics Incorporated
3  * Copyright (c) 2011 Unixphere
4  *
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  */
9 
10 #include <linux/i2c.h>
11 #include <linux/rmi.h>
12 #include <linux/of.h>
13 #include <linux/delay.h>
14 #include <linux/regulator/consumer.h>
15 #include "rmi_driver.h"
16 
17 #define BUFFER_SIZE_INCREMENT 32
18 
19 /**
20  * struct rmi_i2c_xport - stores information for i2c communication
21  *
22  * @xport: The transport interface structure
23  *
24  * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
25  * @page: Keeps track of the current virtual page
26  *
27  * @tx_buf: Buffer used for transmitting data to the sensor over i2c.
28  * @tx_buf_size: Size of the buffer
29  */
30 struct rmi_i2c_xport {
31 	struct rmi_transport_dev xport;
32 	struct i2c_client *client;
33 
34 	struct mutex page_mutex;
35 	int page;
36 
37 	u8 *tx_buf;
38 	size_t tx_buf_size;
39 
40 	struct regulator_bulk_data supplies[2];
41 	u32 startup_delay;
42 };
43 
44 #define RMI_PAGE_SELECT_REGISTER 0xff
45 #define RMI_I2C_PAGE(addr) (((addr) >> 8) & 0xff)
46 
47 /*
48  * rmi_set_page - Set RMI page
49  * @xport: The pointer to the rmi_transport_dev struct
50  * @page: The new page address.
51  *
52  * RMI devices have 16-bit addressing, but some of the transport
53  * implementations (like SMBus) only have 8-bit addressing. So RMI implements
54  * a page address at 0xff of every page so we can reliable page addresses
55  * every 256 registers.
56  *
57  * The page_mutex lock must be held when this function is entered.
58  *
59  * Returns zero on success, non-zero on failure.
60  */
61 static int rmi_set_page(struct rmi_i2c_xport *rmi_i2c, u8 page)
62 {
63 	struct i2c_client *client = rmi_i2c->client;
64 	u8 txbuf[2] = {RMI_PAGE_SELECT_REGISTER, page};
65 	int retval;
66 
67 	retval = i2c_master_send(client, txbuf, sizeof(txbuf));
68 	if (retval != sizeof(txbuf)) {
69 		dev_err(&client->dev,
70 			"%s: set page failed: %d.", __func__, retval);
71 		return (retval < 0) ? retval : -EIO;
72 	}
73 
74 	rmi_i2c->page = page;
75 	return 0;
76 }
77 
78 static int rmi_i2c_write_block(struct rmi_transport_dev *xport, u16 addr,
79 			       const void *buf, size_t len)
80 {
81 	struct rmi_i2c_xport *rmi_i2c =
82 		container_of(xport, struct rmi_i2c_xport, xport);
83 	struct i2c_client *client = rmi_i2c->client;
84 	size_t tx_size = len + 1;
85 	int retval;
86 
87 	mutex_lock(&rmi_i2c->page_mutex);
88 
89 	if (!rmi_i2c->tx_buf || rmi_i2c->tx_buf_size < tx_size) {
90 		if (rmi_i2c->tx_buf)
91 			devm_kfree(&client->dev, rmi_i2c->tx_buf);
92 		rmi_i2c->tx_buf_size = tx_size + BUFFER_SIZE_INCREMENT;
93 		rmi_i2c->tx_buf = devm_kzalloc(&client->dev,
94 					       rmi_i2c->tx_buf_size,
95 					       GFP_KERNEL);
96 		if (!rmi_i2c->tx_buf) {
97 			rmi_i2c->tx_buf_size = 0;
98 			retval = -ENOMEM;
99 			goto exit;
100 		}
101 	}
102 
103 	rmi_i2c->tx_buf[0] = addr & 0xff;
104 	memcpy(rmi_i2c->tx_buf + 1, buf, len);
105 
106 	if (RMI_I2C_PAGE(addr) != rmi_i2c->page) {
107 		retval = rmi_set_page(rmi_i2c, RMI_I2C_PAGE(addr));
108 		if (retval)
109 			goto exit;
110 	}
111 
112 	retval = i2c_master_send(client, rmi_i2c->tx_buf, tx_size);
113 	if (retval == tx_size)
114 		retval = 0;
115 	else if (retval >= 0)
116 		retval = -EIO;
117 
118 exit:
119 	rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
120 		"write %zd bytes at %#06x: %d (%*ph)\n",
121 		len, addr, retval, (int)len, buf);
122 
123 	mutex_unlock(&rmi_i2c->page_mutex);
124 	return retval;
125 }
126 
127 static int rmi_i2c_read_block(struct rmi_transport_dev *xport, u16 addr,
128 			      void *buf, size_t len)
129 {
130 	struct rmi_i2c_xport *rmi_i2c =
131 		container_of(xport, struct rmi_i2c_xport, xport);
132 	struct i2c_client *client = rmi_i2c->client;
133 	u8 addr_offset = addr & 0xff;
134 	int retval;
135 	struct i2c_msg msgs[] = {
136 		{
137 			.addr	= client->addr,
138 			.len	= sizeof(addr_offset),
139 			.buf	= &addr_offset,
140 		},
141 		{
142 			.addr	= client->addr,
143 			.flags	= I2C_M_RD,
144 			.len	= len,
145 			.buf	= buf,
146 		},
147 	};
148 
149 	mutex_lock(&rmi_i2c->page_mutex);
150 
151 	if (RMI_I2C_PAGE(addr) != rmi_i2c->page) {
152 		retval = rmi_set_page(rmi_i2c, RMI_I2C_PAGE(addr));
153 		if (retval)
154 			goto exit;
155 	}
156 
157 	retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
158 	if (retval == ARRAY_SIZE(msgs))
159 		retval = 0; /* success */
160 	else if (retval >= 0)
161 		retval = -EIO;
162 
163 exit:
164 	rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
165 		"read %zd bytes at %#06x: %d (%*ph)\n",
166 		len, addr, retval, (int)len, buf);
167 
168 	mutex_unlock(&rmi_i2c->page_mutex);
169 	return retval;
170 }
171 
172 static const struct rmi_transport_ops rmi_i2c_ops = {
173 	.write_block	= rmi_i2c_write_block,
174 	.read_block	= rmi_i2c_read_block,
175 };
176 
177 #ifdef CONFIG_OF
178 static const struct of_device_id rmi_i2c_of_match[] = {
179 	{ .compatible = "syna,rmi4-i2c" },
180 	{},
181 };
182 MODULE_DEVICE_TABLE(of, rmi_i2c_of_match);
183 #endif
184 
185 static void rmi_i2c_regulator_bulk_disable(void *data)
186 {
187 	struct rmi_i2c_xport *rmi_i2c = data;
188 
189 	regulator_bulk_disable(ARRAY_SIZE(rmi_i2c->supplies),
190 			       rmi_i2c->supplies);
191 }
192 
193 static void rmi_i2c_unregister_transport(void *data)
194 {
195 	struct rmi_i2c_xport *rmi_i2c = data;
196 
197 	rmi_unregister_transport_device(&rmi_i2c->xport);
198 }
199 
200 static int rmi_i2c_probe(struct i2c_client *client,
201 			 const struct i2c_device_id *id)
202 {
203 	struct rmi_device_platform_data *pdata;
204 	struct rmi_device_platform_data *client_pdata =
205 					dev_get_platdata(&client->dev);
206 	struct rmi_i2c_xport *rmi_i2c;
207 	int retval;
208 
209 	rmi_i2c = devm_kzalloc(&client->dev, sizeof(struct rmi_i2c_xport),
210 				GFP_KERNEL);
211 	if (!rmi_i2c)
212 		return -ENOMEM;
213 
214 	pdata = &rmi_i2c->xport.pdata;
215 
216 	if (!client->dev.of_node && client_pdata)
217 		*pdata = *client_pdata;
218 
219 	pdata->irq = client->irq;
220 
221 	rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s.\n",
222 			dev_name(&client->dev));
223 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
224 		dev_err(&client->dev,
225 			"adapter does not support required functionality.\n");
226 		return -ENODEV;
227 	}
228 
229 	rmi_i2c->supplies[0].supply = "vdd";
230 	rmi_i2c->supplies[1].supply = "vio";
231 	retval = devm_regulator_bulk_get(&client->dev,
232 					 ARRAY_SIZE(rmi_i2c->supplies),
233 					 rmi_i2c->supplies);
234 	if (retval < 0)
235 		return retval;
236 
237 	retval = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
238 				       rmi_i2c->supplies);
239 	if (retval < 0)
240 		return retval;
241 
242 	retval = devm_add_action_or_reset(&client->dev,
243 					  rmi_i2c_regulator_bulk_disable,
244 					  rmi_i2c);
245 	if (retval)
246 		return retval;
247 
248 	of_property_read_u32(client->dev.of_node, "syna,startup-delay-ms",
249 			     &rmi_i2c->startup_delay);
250 
251 	msleep(rmi_i2c->startup_delay);
252 
253 	rmi_i2c->client = client;
254 	mutex_init(&rmi_i2c->page_mutex);
255 
256 	rmi_i2c->xport.dev = &client->dev;
257 	rmi_i2c->xport.proto_name = "i2c";
258 	rmi_i2c->xport.ops = &rmi_i2c_ops;
259 
260 	i2c_set_clientdata(client, rmi_i2c);
261 
262 	/*
263 	 * Setting the page to zero will (a) make sure the PSR is in a
264 	 * known state, and (b) make sure we can talk to the device.
265 	 */
266 	retval = rmi_set_page(rmi_i2c, 0);
267 	if (retval) {
268 		dev_err(&client->dev, "Failed to set page select to 0.\n");
269 		return retval;
270 	}
271 
272 	retval = rmi_register_transport_device(&rmi_i2c->xport);
273 	if (retval) {
274 		dev_err(&client->dev, "Failed to register transport driver at 0x%.2X.\n",
275 			client->addr);
276 		return retval;
277 	}
278 	retval = devm_add_action_or_reset(&client->dev,
279 					  rmi_i2c_unregister_transport,
280 					  rmi_i2c);
281 	if (retval)
282 		return retval;
283 
284 	dev_info(&client->dev, "registered rmi i2c driver at %#04x.\n",
285 			client->addr);
286 	return 0;
287 }
288 
289 #ifdef CONFIG_PM_SLEEP
290 static int rmi_i2c_suspend(struct device *dev)
291 {
292 	struct i2c_client *client = to_i2c_client(dev);
293 	struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
294 	int ret;
295 
296 	ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev, true);
297 	if (ret)
298 		dev_warn(dev, "Failed to resume device: %d\n", ret);
299 
300 	regulator_bulk_disable(ARRAY_SIZE(rmi_i2c->supplies),
301 			       rmi_i2c->supplies);
302 
303 	return ret;
304 }
305 
306 static int rmi_i2c_resume(struct device *dev)
307 {
308 	struct i2c_client *client = to_i2c_client(dev);
309 	struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
310 	int ret;
311 
312 	ret = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
313 				    rmi_i2c->supplies);
314 	if (ret)
315 		return ret;
316 
317 	msleep(rmi_i2c->startup_delay);
318 
319 	ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev, true);
320 	if (ret)
321 		dev_warn(dev, "Failed to resume device: %d\n", ret);
322 
323 	return ret;
324 }
325 #endif
326 
327 #ifdef CONFIG_PM
328 static int rmi_i2c_runtime_suspend(struct device *dev)
329 {
330 	struct i2c_client *client = to_i2c_client(dev);
331 	struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
332 	int ret;
333 
334 	ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev, false);
335 	if (ret)
336 		dev_warn(dev, "Failed to resume device: %d\n", ret);
337 
338 	regulator_bulk_disable(ARRAY_SIZE(rmi_i2c->supplies),
339 			       rmi_i2c->supplies);
340 
341 	return 0;
342 }
343 
344 static int rmi_i2c_runtime_resume(struct device *dev)
345 {
346 	struct i2c_client *client = to_i2c_client(dev);
347 	struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
348 	int ret;
349 
350 	ret = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
351 				    rmi_i2c->supplies);
352 	if (ret)
353 		return ret;
354 
355 	msleep(rmi_i2c->startup_delay);
356 
357 	ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev, false);
358 	if (ret)
359 		dev_warn(dev, "Failed to resume device: %d\n", ret);
360 
361 	return 0;
362 }
363 #endif
364 
365 static const struct dev_pm_ops rmi_i2c_pm = {
366 	SET_SYSTEM_SLEEP_PM_OPS(rmi_i2c_suspend, rmi_i2c_resume)
367 	SET_RUNTIME_PM_OPS(rmi_i2c_runtime_suspend, rmi_i2c_runtime_resume,
368 			   NULL)
369 };
370 
371 static const struct i2c_device_id rmi_id[] = {
372 	{ "rmi4_i2c", 0 },
373 	{ }
374 };
375 MODULE_DEVICE_TABLE(i2c, rmi_id);
376 
377 static struct i2c_driver rmi_i2c_driver = {
378 	.driver = {
379 		.name	= "rmi4_i2c",
380 		.pm	= &rmi_i2c_pm,
381 		.of_match_table = of_match_ptr(rmi_i2c_of_match),
382 	},
383 	.id_table	= rmi_id,
384 	.probe		= rmi_i2c_probe,
385 };
386 
387 module_i2c_driver(rmi_i2c_driver);
388 
389 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>");
390 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
391 MODULE_DESCRIPTION("RMI I2C driver");
392 MODULE_LICENSE("GPL");
393 MODULE_VERSION(RMI_DRIVER_VERSION);
394