blob: 992b08dd6120257cf9ce7e621c674dd6efbf8f3c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mateusz Kulikowskic7e4fbb2016-03-31 23:12:28 +02002/*
3 * Sample SPMI bus driver
4 *
5 * It emulates bus with single pm8916-like pmic that has only GPIO reigsters.
6 *
7 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
Mateusz Kulikowskic7e4fbb2016-03-31 23:12:28 +02008 */
9
Mateusz Kulikowskic7e4fbb2016-03-31 23:12:28 +020010#include <dm.h>
11#include <errno.h>
12#include <spmi/spmi.h>
13#include <asm/gpio.h>
14#include <asm/io.h>
15
Mateusz Kulikowskic7e4fbb2016-03-31 23:12:28 +020016#define EMUL_GPIO_PID_START 0xC0
17#define EMUL_GPIO_PID_END 0xC3
18
19#define EMUL_GPIO_COUNT 4
20
21#define EMUL_GPIO_REG_END 0x46 /* Last valid register */
22
23#define EMUL_PERM_R 0x1
24#define EMUL_PERM_W 0x2
25#define EMUL_PERM_RW (EMUL_PERM_R | EMUL_PERM_W)
26
27struct sandbox_emul_fake_regs {
28 u8 value;
29 u8 access_mask;
30 u8 perms; /* Access permissions */
31};
32
33struct sandbox_emul_gpio {
Mateusz Kulikowski7b9199f2016-04-04 19:55:58 +020034 /* Fake registers - need one more entry as REG_END is valid address. */
35 struct sandbox_emul_fake_regs r[EMUL_GPIO_REG_END + 1];
Mateusz Kulikowskic7e4fbb2016-03-31 23:12:28 +020036};
37
38struct sandbox_spmi_priv {
39 struct sandbox_emul_gpio gpios[EMUL_GPIO_COUNT];
40};
41
42/* Check if valid register was requested */
43static bool check_address_valid(int usid, int pid, int off)
44{
45 if (usid != 0)
46 return false;
47 if (pid < EMUL_GPIO_PID_START || pid > EMUL_GPIO_PID_END)
48 return false;
49 if (off > EMUL_GPIO_REG_END)
50 return false;
51 return true;
52}
53
54static int sandbox_spmi_write(struct udevice *dev, int usid, int pid, int off,
55 uint8_t val)
56{
57 struct sandbox_spmi_priv *priv = dev_get_priv(dev);
58 struct sandbox_emul_fake_regs *regs;
59
60 if (!check_address_valid(usid, pid, off))
61 return -EIO;
62
63 regs = priv->gpios[pid & 0x3].r; /* Last 3 bits of pid are gpio # */
64
65 switch (off) {
66 case 0x40: /* Control */
67 val &= regs[off].access_mask;
68 if (((val & 0x30) == 0x10) || ((val & 0x30) == 0x20)) {
69 /* out/inout - set status register */
70 regs[0x8].value &= ~0x1;
71 regs[0x8].value |= val & 0x1;
72 }
73 break;
74 default:
75 if (regs[off].perms & EMUL_PERM_W)
76 regs[off].value = val & regs[off].access_mask;
77 }
78 return 0;
79}
80
81static int sandbox_spmi_read(struct udevice *dev, int usid, int pid, int off)
82{
83 struct sandbox_spmi_priv *priv = dev_get_priv(dev);
84 struct sandbox_emul_fake_regs *regs;
85
86 if (!check_address_valid(usid, pid, off))
87 return -EIO;
88
89 regs = priv->gpios[pid & 0x3].r; /* Last 3 bits of pid are gpio # */
90
91 if (regs[0x46].value == 0) /* Block disabled */
92 return 0;
93
94 switch (off) {
95 case 0x8: /* Status */
96 if (regs[0x46].value == 0) /* Block disabled */
97 return 0;
98 return regs[off].value;
99 default:
100 if (regs[off].perms & EMUL_PERM_R)
101 return regs[off].value;
102 else
103 return 0;
104 }
105}
106
107static struct dm_spmi_ops sandbox_spmi_ops = {
108 .read = sandbox_spmi_read,
109 .write = sandbox_spmi_write,
110};
111
112static int sandbox_spmi_probe(struct udevice *dev)
113{
114 struct sandbox_spmi_priv *priv = dev_get_priv(dev);
115 int i;
116
117 for (i = 0; i < EMUL_GPIO_COUNT; ++i) {
118 struct sandbox_emul_fake_regs *regs = priv->gpios[i].r;
119 regs[4].perms = EMUL_PERM_R;
120 regs[4].value = 0x10;
121 regs[5].perms = EMUL_PERM_R;
122 regs[5].value = 0x5;
123 regs[8].access_mask = 0x81;
124 regs[8].perms = EMUL_PERM_RW;
125 regs[0x40].access_mask = 0x7F;
126 regs[0x40].perms = EMUL_PERM_RW;
127 regs[0x41].access_mask = 7;
128 regs[0x41].perms = EMUL_PERM_RW;
129 regs[0x42].access_mask = 7;
130 regs[0x42].perms = EMUL_PERM_RW;
131 regs[0x42].value = 0x4;
132 regs[0x45].access_mask = 0x3F;
133 regs[0x45].perms = EMUL_PERM_RW;
134 regs[0x45].value = 0x1;
135 regs[0x46].access_mask = 0x80;
136 regs[0x46].perms = EMUL_PERM_RW;
137 regs[0x46].value = 0x80;
138 }
139 return 0;
140}
141
142static const struct udevice_id sandbox_spmi_ids[] = {
143 { .compatible = "sandbox,spmi" },
144 { }
145};
146
147U_BOOT_DRIVER(msm_spmi) = {
148 .name = "sandbox_spmi",
149 .id = UCLASS_SPMI,
150 .of_match = sandbox_spmi_ids,
151 .ops = &sandbox_spmi_ops,
152 .probe = sandbox_spmi_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700153 .priv_auto = sizeof(struct sandbox_spmi_priv),
Mateusz Kulikowskic7e4fbb2016-03-31 23:12:28 +0200154};