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