Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 2 | /* |
| 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 Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <spmi/spmi.h> |
| 13 | #include <asm/gpio.h> |
| 14 | #include <asm/io.h> |
| 15 | |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 16 | #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 | |
| 27 | struct sandbox_emul_fake_regs { |
| 28 | u8 value; |
| 29 | u8 access_mask; |
| 30 | u8 perms; /* Access permissions */ |
| 31 | }; |
| 32 | |
| 33 | struct sandbox_emul_gpio { |
Mateusz Kulikowski | 7b9199f | 2016-04-04 19:55:58 +0200 | [diff] [blame] | 34 | /* 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 Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct sandbox_spmi_priv { |
| 39 | struct sandbox_emul_gpio gpios[EMUL_GPIO_COUNT]; |
| 40 | }; |
| 41 | |
| 42 | /* Check if valid register was requested */ |
| 43 | static 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 | |
| 54 | static 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 | |
| 81 | static 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 | |
| 107 | static struct dm_spmi_ops sandbox_spmi_ops = { |
| 108 | .read = sandbox_spmi_read, |
| 109 | .write = sandbox_spmi_write, |
| 110 | }; |
| 111 | |
| 112 | static 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 | |
| 142 | static const struct udevice_id sandbox_spmi_ids[] = { |
| 143 | { .compatible = "sandbox,spmi" }, |
| 144 | { } |
| 145 | }; |
| 146 | |
| 147 | U_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 Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 153 | .priv_auto = sizeof(struct sandbox_spmi_priv), |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 154 | }; |