1 /*
2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/idr.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/spmi.h>
22 #include <linux/pm_runtime.h>
23
24 #include <dt-bindings/spmi/spmi.h>
25 #define CREATE_TRACE_POINTS
26 #include <trace/events/spmi.h>
27
28 static DEFINE_IDA(ctrl_ida);
29
spmi_dev_release(struct device * dev)30 static void spmi_dev_release(struct device *dev)
31 {
32 struct spmi_device *sdev = to_spmi_device(dev);
33 kfree(sdev);
34 }
35
36 static const struct device_type spmi_dev_type = {
37 .release = spmi_dev_release,
38 };
39
spmi_ctrl_release(struct device * dev)40 static void spmi_ctrl_release(struct device *dev)
41 {
42 struct spmi_controller *ctrl = to_spmi_controller(dev);
43 ida_simple_remove(&ctrl_ida, ctrl->nr);
44 kfree(ctrl);
45 }
46
47 static const struct device_type spmi_ctrl_type = {
48 .release = spmi_ctrl_release,
49 };
50
spmi_device_match(struct device * dev,struct device_driver * drv)51 static int spmi_device_match(struct device *dev, struct device_driver *drv)
52 {
53 if (of_driver_match_device(dev, drv))
54 return 1;
55
56 if (drv->name)
57 return strncmp(dev_name(dev), drv->name,
58 SPMI_NAME_SIZE) == 0;
59
60 return 0;
61 }
62
63 /**
64 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
65 * @sdev: spmi_device to be added
66 */
spmi_device_add(struct spmi_device * sdev)67 int spmi_device_add(struct spmi_device *sdev)
68 {
69 struct spmi_controller *ctrl = sdev->ctrl;
70 int err;
71
72 dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
73
74 err = device_add(&sdev->dev);
75 if (err < 0) {
76 dev_err(&sdev->dev, "Can't add %s, status %d\n",
77 dev_name(&sdev->dev), err);
78 goto err_device_add;
79 }
80
81 dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
82
83 err_device_add:
84 return err;
85 }
86 EXPORT_SYMBOL_GPL(spmi_device_add);
87
88 /**
89 * spmi_device_remove(): remove an SPMI device
90 * @sdev: spmi_device to be removed
91 */
spmi_device_remove(struct spmi_device * sdev)92 void spmi_device_remove(struct spmi_device *sdev)
93 {
94 device_unregister(&sdev->dev);
95 }
96 EXPORT_SYMBOL_GPL(spmi_device_remove);
97
98 static inline int
spmi_cmd(struct spmi_controller * ctrl,u8 opcode,u8 sid)99 spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
100 {
101 int ret;
102
103 if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
104 return -EINVAL;
105
106 ret = ctrl->cmd(ctrl, opcode, sid);
107 trace_spmi_cmd(opcode, sid, ret);
108 return ret;
109 }
110
spmi_read_cmd(struct spmi_controller * ctrl,u8 opcode,u8 sid,u16 addr,u8 * buf,size_t len)111 static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
112 u8 sid, u16 addr, u8 *buf, size_t len)
113 {
114 int ret;
115
116 if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
117 return -EINVAL;
118
119 trace_spmi_read_begin(opcode, sid, addr);
120 ret = ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
121 trace_spmi_read_end(opcode, sid, addr, ret, len, buf);
122 return ret;
123 }
124
spmi_write_cmd(struct spmi_controller * ctrl,u8 opcode,u8 sid,u16 addr,const u8 * buf,size_t len)125 static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
126 u8 sid, u16 addr, const u8 *buf, size_t len)
127 {
128 int ret;
129
130 if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
131 return -EINVAL;
132
133 trace_spmi_write_begin(opcode, sid, addr, len, buf);
134 ret = ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
135 trace_spmi_write_end(opcode, sid, addr, ret);
136 return ret;
137 }
138
139 /**
140 * spmi_register_read() - register read
141 * @sdev: SPMI device.
142 * @addr: slave register address (5-bit address).
143 * @buf: buffer to be populated with data from the Slave.
144 *
145 * Reads 1 byte of data from a Slave device register.
146 */
spmi_register_read(struct spmi_device * sdev,u8 addr,u8 * buf)147 int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
148 {
149 /* 5-bit register address */
150 if (addr > 0x1F)
151 return -EINVAL;
152
153 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
154 buf, 1);
155 }
156 EXPORT_SYMBOL_GPL(spmi_register_read);
157
158 /**
159 * spmi_ext_register_read() - extended register read
160 * @sdev: SPMI device.
161 * @addr: slave register address (8-bit address).
162 * @buf: buffer to be populated with data from the Slave.
163 * @len: the request number of bytes to read (up to 16 bytes).
164 *
165 * Reads up to 16 bytes of data from the extended register space on a
166 * Slave device.
167 */
spmi_ext_register_read(struct spmi_device * sdev,u8 addr,u8 * buf,size_t len)168 int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
169 size_t len)
170 {
171 /* 8-bit register address, up to 16 bytes */
172 if (len == 0 || len > 16)
173 return -EINVAL;
174
175 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
176 buf, len);
177 }
178 EXPORT_SYMBOL_GPL(spmi_ext_register_read);
179
180 /**
181 * spmi_ext_register_readl() - extended register read long
182 * @sdev: SPMI device.
183 * @addr: slave register address (16-bit address).
184 * @buf: buffer to be populated with data from the Slave.
185 * @len: the request number of bytes to read (up to 8 bytes).
186 *
187 * Reads up to 8 bytes of data from the extended register space on a
188 * Slave device using 16-bit address.
189 */
spmi_ext_register_readl(struct spmi_device * sdev,u16 addr,u8 * buf,size_t len)190 int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
191 size_t len)
192 {
193 /* 16-bit register address, up to 8 bytes */
194 if (len == 0 || len > 8)
195 return -EINVAL;
196
197 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
198 buf, len);
199 }
200 EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
201
202 /**
203 * spmi_register_write() - register write
204 * @sdev: SPMI device
205 * @addr: slave register address (5-bit address).
206 * @data: buffer containing the data to be transferred to the Slave.
207 *
208 * Writes 1 byte of data to a Slave device register.
209 */
spmi_register_write(struct spmi_device * sdev,u8 addr,u8 data)210 int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
211 {
212 /* 5-bit register address */
213 if (addr > 0x1F)
214 return -EINVAL;
215
216 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
217 &data, 1);
218 }
219 EXPORT_SYMBOL_GPL(spmi_register_write);
220
221 /**
222 * spmi_register_zero_write() - register zero write
223 * @sdev: SPMI device.
224 * @data: the data to be written to register 0 (7-bits).
225 *
226 * Writes data to register 0 of the Slave device.
227 */
spmi_register_zero_write(struct spmi_device * sdev,u8 data)228 int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
229 {
230 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
231 &data, 1);
232 }
233 EXPORT_SYMBOL_GPL(spmi_register_zero_write);
234
235 /**
236 * spmi_ext_register_write() - extended register write
237 * @sdev: SPMI device.
238 * @addr: slave register address (8-bit address).
239 * @buf: buffer containing the data to be transferred to the Slave.
240 * @len: the request number of bytes to read (up to 16 bytes).
241 *
242 * Writes up to 16 bytes of data to the extended register space of a
243 * Slave device.
244 */
spmi_ext_register_write(struct spmi_device * sdev,u8 addr,const u8 * buf,size_t len)245 int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
246 size_t len)
247 {
248 /* 8-bit register address, up to 16 bytes */
249 if (len == 0 || len > 16)
250 return -EINVAL;
251
252 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
253 buf, len);
254 }
255 EXPORT_SYMBOL_GPL(spmi_ext_register_write);
256
257 /**
258 * spmi_ext_register_writel() - extended register write long
259 * @sdev: SPMI device.
260 * @addr: slave register address (16-bit address).
261 * @buf: buffer containing the data to be transferred to the Slave.
262 * @len: the request number of bytes to read (up to 8 bytes).
263 *
264 * Writes up to 8 bytes of data to the extended register space of a
265 * Slave device using 16-bit address.
266 */
spmi_ext_register_writel(struct spmi_device * sdev,u16 addr,const u8 * buf,size_t len)267 int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
268 size_t len)
269 {
270 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
271 if (len == 0 || len > 8)
272 return -EINVAL;
273
274 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
275 addr, buf, len);
276 }
277 EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
278
279 /**
280 * spmi_command_reset() - sends RESET command to the specified slave
281 * @sdev: SPMI device.
282 *
283 * The Reset command initializes the Slave and forces all registers to
284 * their reset values. The Slave shall enter the STARTUP state after
285 * receiving a Reset command.
286 */
spmi_command_reset(struct spmi_device * sdev)287 int spmi_command_reset(struct spmi_device *sdev)
288 {
289 return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
290 }
291 EXPORT_SYMBOL_GPL(spmi_command_reset);
292
293 /**
294 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
295 * @sdev: SPMI device.
296 *
297 * The Sleep command causes the Slave to enter the user defined SLEEP state.
298 */
spmi_command_sleep(struct spmi_device * sdev)299 int spmi_command_sleep(struct spmi_device *sdev)
300 {
301 return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
302 }
303 EXPORT_SYMBOL_GPL(spmi_command_sleep);
304
305 /**
306 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
307 * @sdev: SPMI device.
308 *
309 * The Wakeup command causes the Slave to move from the SLEEP state to
310 * the ACTIVE state.
311 */
spmi_command_wakeup(struct spmi_device * sdev)312 int spmi_command_wakeup(struct spmi_device *sdev)
313 {
314 return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
315 }
316 EXPORT_SYMBOL_GPL(spmi_command_wakeup);
317
318 /**
319 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
320 * @sdev: SPMI device.
321 *
322 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
323 */
spmi_command_shutdown(struct spmi_device * sdev)324 int spmi_command_shutdown(struct spmi_device *sdev)
325 {
326 return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
327 }
328 EXPORT_SYMBOL_GPL(spmi_command_shutdown);
329
spmi_drv_probe(struct device * dev)330 static int spmi_drv_probe(struct device *dev)
331 {
332 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
333 struct spmi_device *sdev = to_spmi_device(dev);
334 int err;
335
336 pm_runtime_get_noresume(dev);
337 pm_runtime_set_active(dev);
338 pm_runtime_enable(dev);
339
340 err = sdrv->probe(sdev);
341 if (err)
342 goto fail_probe;
343
344 return 0;
345
346 fail_probe:
347 pm_runtime_disable(dev);
348 pm_runtime_set_suspended(dev);
349 pm_runtime_put_noidle(dev);
350 return err;
351 }
352
spmi_drv_remove(struct device * dev)353 static int spmi_drv_remove(struct device *dev)
354 {
355 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
356
357 pm_runtime_get_sync(dev);
358 sdrv->remove(to_spmi_device(dev));
359 pm_runtime_put_noidle(dev);
360
361 pm_runtime_disable(dev);
362 pm_runtime_set_suspended(dev);
363 pm_runtime_put_noidle(dev);
364 return 0;
365 }
366
spmi_drv_uevent(struct device * dev,struct kobj_uevent_env * env)367 static int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env)
368 {
369 int ret;
370
371 ret = of_device_uevent_modalias(dev, env);
372 if (ret != -ENODEV)
373 return ret;
374
375 return 0;
376 }
377
378 static struct bus_type spmi_bus_type = {
379 .name = "spmi",
380 .match = spmi_device_match,
381 .probe = spmi_drv_probe,
382 .remove = spmi_drv_remove,
383 .uevent = spmi_drv_uevent,
384 };
385
386 /**
387 * spmi_controller_alloc() - Allocate a new SPMI device
388 * @ctrl: associated controller
389 *
390 * Caller is responsible for either calling spmi_device_add() to add the
391 * newly allocated controller, or calling spmi_device_put() to discard it.
392 */
spmi_device_alloc(struct spmi_controller * ctrl)393 struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
394 {
395 struct spmi_device *sdev;
396
397 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
398 if (!sdev)
399 return NULL;
400
401 sdev->ctrl = ctrl;
402 device_initialize(&sdev->dev);
403 sdev->dev.parent = &ctrl->dev;
404 sdev->dev.bus = &spmi_bus_type;
405 sdev->dev.type = &spmi_dev_type;
406 return sdev;
407 }
408 EXPORT_SYMBOL_GPL(spmi_device_alloc);
409
410 /**
411 * spmi_controller_alloc() - Allocate a new SPMI controller
412 * @parent: parent device
413 * @size: size of private data
414 *
415 * Caller is responsible for either calling spmi_controller_add() to add the
416 * newly allocated controller, or calling spmi_controller_put() to discard it.
417 * The allocated private data region may be accessed via
418 * spmi_controller_get_drvdata()
419 */
spmi_controller_alloc(struct device * parent,size_t size)420 struct spmi_controller *spmi_controller_alloc(struct device *parent,
421 size_t size)
422 {
423 struct spmi_controller *ctrl;
424 int id;
425
426 if (WARN_ON(!parent))
427 return NULL;
428
429 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
430 if (!ctrl)
431 return NULL;
432
433 device_initialize(&ctrl->dev);
434 ctrl->dev.type = &spmi_ctrl_type;
435 ctrl->dev.bus = &spmi_bus_type;
436 ctrl->dev.parent = parent;
437 ctrl->dev.of_node = parent->of_node;
438 spmi_controller_set_drvdata(ctrl, &ctrl[1]);
439
440 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
441 if (id < 0) {
442 dev_err(parent,
443 "unable to allocate SPMI controller identifier.\n");
444 spmi_controller_put(ctrl);
445 return NULL;
446 }
447
448 ctrl->nr = id;
449 dev_set_name(&ctrl->dev, "spmi-%d", id);
450
451 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
452 return ctrl;
453 }
454 EXPORT_SYMBOL_GPL(spmi_controller_alloc);
455
of_spmi_register_devices(struct spmi_controller * ctrl)456 static void of_spmi_register_devices(struct spmi_controller *ctrl)
457 {
458 struct device_node *node;
459 int err;
460
461 if (!ctrl->dev.of_node)
462 return;
463
464 for_each_available_child_of_node(ctrl->dev.of_node, node) {
465 struct spmi_device *sdev;
466 u32 reg[2];
467
468 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
469
470 err = of_property_read_u32_array(node, "reg", reg, 2);
471 if (err) {
472 dev_err(&ctrl->dev,
473 "node %s err (%d) does not have 'reg' property\n",
474 node->full_name, err);
475 continue;
476 }
477
478 if (reg[1] != SPMI_USID) {
479 dev_err(&ctrl->dev,
480 "node %s contains unsupported 'reg' entry\n",
481 node->full_name);
482 continue;
483 }
484
485 if (reg[0] >= SPMI_MAX_SLAVE_ID) {
486 dev_err(&ctrl->dev,
487 "invalid usid on node %s\n",
488 node->full_name);
489 continue;
490 }
491
492 dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
493
494 sdev = spmi_device_alloc(ctrl);
495 if (!sdev)
496 continue;
497
498 sdev->dev.of_node = node;
499 sdev->usid = (u8) reg[0];
500
501 err = spmi_device_add(sdev);
502 if (err) {
503 dev_err(&sdev->dev,
504 "failure adding device. status %d\n", err);
505 spmi_device_put(sdev);
506 }
507 }
508 }
509
510 /**
511 * spmi_controller_add() - Add an SPMI controller
512 * @ctrl: controller to be registered.
513 *
514 * Register a controller previously allocated via spmi_controller_alloc() with
515 * the SPMI core.
516 */
spmi_controller_add(struct spmi_controller * ctrl)517 int spmi_controller_add(struct spmi_controller *ctrl)
518 {
519 int ret;
520
521 /* Can't register until after driver model init */
522 if (WARN_ON(!spmi_bus_type.p))
523 return -EAGAIN;
524
525 ret = device_add(&ctrl->dev);
526 if (ret)
527 return ret;
528
529 if (IS_ENABLED(CONFIG_OF))
530 of_spmi_register_devices(ctrl);
531
532 dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
533 ctrl->nr, &ctrl->dev);
534
535 return 0;
536 };
537 EXPORT_SYMBOL_GPL(spmi_controller_add);
538
539 /* Remove a device associated with a controller */
spmi_ctrl_remove_device(struct device * dev,void * data)540 static int spmi_ctrl_remove_device(struct device *dev, void *data)
541 {
542 struct spmi_device *spmidev = to_spmi_device(dev);
543 if (dev->type == &spmi_dev_type)
544 spmi_device_remove(spmidev);
545 return 0;
546 }
547
548 /**
549 * spmi_controller_remove(): remove an SPMI controller
550 * @ctrl: controller to remove
551 *
552 * Remove a SPMI controller. Caller is responsible for calling
553 * spmi_controller_put() to discard the allocated controller.
554 */
spmi_controller_remove(struct spmi_controller * ctrl)555 void spmi_controller_remove(struct spmi_controller *ctrl)
556 {
557 int dummy;
558
559 if (!ctrl)
560 return;
561
562 dummy = device_for_each_child(&ctrl->dev, NULL,
563 spmi_ctrl_remove_device);
564 device_del(&ctrl->dev);
565 }
566 EXPORT_SYMBOL_GPL(spmi_controller_remove);
567
568 /**
569 * spmi_driver_register() - Register client driver with SPMI core
570 * @sdrv: client driver to be associated with client-device.
571 *
572 * This API will register the client driver with the SPMI framework.
573 * It is typically called from the driver's module-init function.
574 */
__spmi_driver_register(struct spmi_driver * sdrv,struct module * owner)575 int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
576 {
577 sdrv->driver.bus = &spmi_bus_type;
578 sdrv->driver.owner = owner;
579 return driver_register(&sdrv->driver);
580 }
581 EXPORT_SYMBOL_GPL(__spmi_driver_register);
582
spmi_exit(void)583 static void __exit spmi_exit(void)
584 {
585 bus_unregister(&spmi_bus_type);
586 }
587 module_exit(spmi_exit);
588
spmi_init(void)589 static int __init spmi_init(void)
590 {
591 return bus_register(&spmi_bus_type);
592 }
593 postcore_initcall(spmi_init);
594
595 MODULE_LICENSE("GPL v2");
596 MODULE_DESCRIPTION("SPMI module");
597 MODULE_ALIAS("platform:spmi");
598