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