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