1
2
3
4
5
6
7
8
9
10
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
25static DEFINE_IDA(ctrl_ida);
26
27static void spmi_dev_release(struct device *dev)
28{
29 struct spmi_device *sdev = to_spmi_device(dev);
30 kfree(sdev);
31}
32
33static const struct device_type spmi_dev_type = {
34 .release = spmi_dev_release,
35};
36
37static 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
44static const struct device_type spmi_ctrl_type = {
45 .release = spmi_ctrl_release,
46};
47
48static 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
62
63
64int 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
80err_device_add:
81 return err;
82}
83EXPORT_SYMBOL_GPL(spmi_device_add);
84
85
86
87
88
89void spmi_device_remove(struct spmi_device *sdev)
90{
91 device_unregister(&sdev->dev);
92}
93EXPORT_SYMBOL_GPL(spmi_device_remove);
94
95static inline int
96spmi_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
104static 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
113static 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
124
125
126
127
128
129
130int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
131{
132
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}
139EXPORT_SYMBOL_GPL(spmi_register_read);
140
141
142
143
144
145
146
147
148
149
150
151int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
152 size_t len)
153{
154
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}
161EXPORT_SYMBOL_GPL(spmi_ext_register_read);
162
163
164
165
166
167
168
169
170
171
172
173int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
174 size_t len)
175{
176
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}
183EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
184
185
186
187
188
189
190
191
192
193int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
194{
195
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}
202EXPORT_SYMBOL_GPL(spmi_register_write);
203
204
205
206
207
208
209
210
211int 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}
216EXPORT_SYMBOL_GPL(spmi_register_zero_write);
217
218
219
220
221
222
223
224
225
226
227
228int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
229 size_t len)
230{
231
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}
238EXPORT_SYMBOL_GPL(spmi_ext_register_write);
239
240
241
242
243
244
245
246
247
248
249
250int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
251 size_t len)
252{
253
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}
260EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
261
262
263
264
265
266
267
268
269
270int spmi_command_reset(struct spmi_device *sdev)
271{
272 return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
273}
274EXPORT_SYMBOL_GPL(spmi_command_reset);
275
276
277
278
279
280
281
282int spmi_command_sleep(struct spmi_device *sdev)
283{
284 return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
285}
286EXPORT_SYMBOL_GPL(spmi_command_sleep);
287
288
289
290
291
292
293
294
295int spmi_command_wakeup(struct spmi_device *sdev)
296{
297 return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
298}
299EXPORT_SYMBOL_GPL(spmi_command_wakeup);
300
301
302
303
304
305
306
307int spmi_command_shutdown(struct spmi_device *sdev)
308{
309 return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
310}
311EXPORT_SYMBOL_GPL(spmi_command_shutdown);
312
313static 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
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
334fail_probe:
335 pm_runtime_disable(dev);
336 pm_runtime_set_suspended(dev);
337 pm_runtime_put_noidle(dev);
338fail_wakeup:
339 return err;
340}
341
342static 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
356static struct bus_type spmi_bus_type = {
357 .name = "spmi",
358 .match = spmi_device_match,
359 .probe = spmi_drv_probe,
360 .remove = spmi_drv_remove,
361};
362
363
364
365
366
367
368
369
370struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
371{
372 struct spmi_device *sdev;
373
374 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
375 if (!sdev)
376 return NULL;
377
378 sdev->ctrl = ctrl;
379 device_initialize(&sdev->dev);
380 sdev->dev.parent = &ctrl->dev;
381 sdev->dev.bus = &spmi_bus_type;
382 sdev->dev.type = &spmi_dev_type;
383 return sdev;
384}
385EXPORT_SYMBOL_GPL(spmi_device_alloc);
386
387
388
389
390
391
392
393
394
395
396
397struct spmi_controller *spmi_controller_alloc(struct device *parent,
398 size_t size)
399{
400 struct spmi_controller *ctrl;
401 int id;
402
403 if (WARN_ON(!parent))
404 return NULL;
405
406 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
407 if (!ctrl)
408 return NULL;
409
410 device_initialize(&ctrl->dev);
411 ctrl->dev.type = &spmi_ctrl_type;
412 ctrl->dev.bus = &spmi_bus_type;
413 ctrl->dev.parent = parent;
414 ctrl->dev.of_node = parent->of_node;
415 spmi_controller_set_drvdata(ctrl, &ctrl[1]);
416
417 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
418 if (id < 0) {
419 dev_err(parent,
420 "unable to allocate SPMI controller identifier.\n");
421 spmi_controller_put(ctrl);
422 return NULL;
423 }
424
425 ctrl->nr = id;
426 dev_set_name(&ctrl->dev, "spmi-%d", id);
427
428 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
429 return ctrl;
430}
431EXPORT_SYMBOL_GPL(spmi_controller_alloc);
432
433static void of_spmi_register_devices(struct spmi_controller *ctrl)
434{
435 struct device_node *node;
436 int err;
437
438 if (!ctrl->dev.of_node)
439 return;
440
441 for_each_available_child_of_node(ctrl->dev.of_node, node) {
442 struct spmi_device *sdev;
443 u32 reg[2];
444
445 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
446
447 err = of_property_read_u32_array(node, "reg", reg, 2);
448 if (err) {
449 dev_err(&ctrl->dev,
450 "node %s err (%d) does not have 'reg' property\n",
451 node->full_name, err);
452 continue;
453 }
454
455 if (reg[1] != SPMI_USID) {
456 dev_err(&ctrl->dev,
457 "node %s contains unsupported 'reg' entry\n",
458 node->full_name);
459 continue;
460 }
461
462 if (reg[0] >= SPMI_MAX_SLAVE_ID) {
463 dev_err(&ctrl->dev,
464 "invalid usid on node %s\n",
465 node->full_name);
466 continue;
467 }
468
469 dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
470
471 sdev = spmi_device_alloc(ctrl);
472 if (!sdev)
473 continue;
474
475 sdev->dev.of_node = node;
476 sdev->usid = (u8) reg[0];
477
478 err = spmi_device_add(sdev);
479 if (err) {
480 dev_err(&sdev->dev,
481 "failure adding device. status %d\n", err);
482 spmi_device_put(sdev);
483 }
484 }
485}
486
487
488
489
490
491
492
493
494int spmi_controller_add(struct spmi_controller *ctrl)
495{
496 int ret;
497
498
499 if (WARN_ON(!spmi_bus_type.p))
500 return -EAGAIN;
501
502 ret = device_add(&ctrl->dev);
503 if (ret)
504 return ret;
505
506 if (IS_ENABLED(CONFIG_OF))
507 of_spmi_register_devices(ctrl);
508
509 dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
510 ctrl->nr, &ctrl->dev);
511
512 return 0;
513};
514EXPORT_SYMBOL_GPL(spmi_controller_add);
515
516
517static int spmi_ctrl_remove_device(struct device *dev, void *data)
518{
519 struct spmi_device *spmidev = to_spmi_device(dev);
520 if (dev->type == &spmi_dev_type)
521 spmi_device_remove(spmidev);
522 return 0;
523}
524
525
526
527
528
529
530
531
532void spmi_controller_remove(struct spmi_controller *ctrl)
533{
534 int dummy;
535
536 if (!ctrl)
537 return;
538
539 dummy = device_for_each_child(&ctrl->dev, NULL,
540 spmi_ctrl_remove_device);
541 device_del(&ctrl->dev);
542}
543EXPORT_SYMBOL_GPL(spmi_controller_remove);
544
545
546
547
548
549
550
551
552int spmi_driver_register(struct spmi_driver *sdrv)
553{
554 sdrv->driver.bus = &spmi_bus_type;
555 return driver_register(&sdrv->driver);
556}
557EXPORT_SYMBOL_GPL(spmi_driver_register);
558
559static void __exit spmi_exit(void)
560{
561 bus_unregister(&spmi_bus_type);
562}
563module_exit(spmi_exit);
564
565static int __init spmi_init(void)
566{
567 return bus_register(&spmi_bus_type);
568}
569postcore_initcall(spmi_init);
570
571MODULE_LICENSE("GPL v2");
572MODULE_DESCRIPTION("SPMI module");
573MODULE_ALIAS("platform:spmi");
574