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