Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Qualcomm generic pmic driver |
| 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
| 6 | */ |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <power/pmic.h> |
| 9 | #include <spmi/spmi.h> |
| 10 | |
| 11 | #define PID_SHIFT 8 |
| 12 | #define PID_MASK (0xFF << PID_SHIFT) |
| 13 | #define REG_MASK 0xFF |
| 14 | |
| 15 | struct pmic_qcom_priv { |
| 16 | uint32_t usid; /* Slave ID on SPMI bus */ |
| 17 | }; |
| 18 | |
| 19 | static int pmic_qcom_reg_count(struct udevice *dev) |
| 20 | { |
| 21 | return 0xFFFF; |
| 22 | } |
| 23 | |
| 24 | static int pmic_qcom_write(struct udevice *dev, uint reg, const uint8_t *buff, |
| 25 | int len) |
| 26 | { |
| 27 | struct pmic_qcom_priv *priv = dev_get_priv(dev); |
| 28 | |
| 29 | if (len != 1) |
| 30 | return -EINVAL; |
| 31 | |
| 32 | return spmi_reg_write(dev->parent, priv->usid, |
| 33 | (reg & PID_MASK) >> PID_SHIFT, reg & REG_MASK, |
| 34 | *buff); |
| 35 | } |
| 36 | |
| 37 | static int pmic_qcom_read(struct udevice *dev, uint reg, uint8_t *buff, int len) |
| 38 | { |
| 39 | struct pmic_qcom_priv *priv = dev_get_priv(dev); |
| 40 | int val; |
| 41 | |
| 42 | if (len != 1) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | val = spmi_reg_read(dev->parent, priv->usid, |
| 46 | (reg & PID_MASK) >> PID_SHIFT, reg & REG_MASK); |
| 47 | |
| 48 | if (val < 0) |
| 49 | return val; |
| 50 | *buff = val; |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static struct dm_pmic_ops pmic_qcom_ops = { |
| 55 | .reg_count = pmic_qcom_reg_count, |
| 56 | .read = pmic_qcom_read, |
| 57 | .write = pmic_qcom_write, |
| 58 | }; |
| 59 | |
| 60 | static const struct udevice_id pmic_qcom_ids[] = { |
| 61 | { .compatible = "qcom,spmi-pmic" }, |
| 62 | { } |
| 63 | }; |
| 64 | |
| 65 | static int pmic_qcom_probe(struct udevice *dev) |
| 66 | { |
| 67 | struct pmic_qcom_priv *priv = dev_get_priv(dev); |
Caleb Connolly | 783cb3f | 2023-12-05 13:46:54 +0000 | [diff] [blame] | 68 | int ret; |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 69 | |
Caleb Connolly | 783cb3f | 2023-12-05 13:46:54 +0000 | [diff] [blame] | 70 | /* |
| 71 | * dev_read_addr() can't be used here because the reg property actually |
| 72 | * contains two discrete values, not a single 64-bit address. |
| 73 | * The address is the first value. |
| 74 | */ |
| 75 | ret = ofnode_read_u32_index(dev_ofnode(dev), "reg", 0, &priv->usid); |
| 76 | if (ret < 0) |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 77 | return -EINVAL; |
| 78 | |
Caleb Connolly | 783cb3f | 2023-12-05 13:46:54 +0000 | [diff] [blame] | 79 | debug("usid: %d\n", priv->usid); |
| 80 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | U_BOOT_DRIVER(pmic_qcom) = { |
| 85 | .name = "pmic_qcom", |
| 86 | .id = UCLASS_PMIC, |
| 87 | .of_match = pmic_qcom_ids, |
| 88 | .bind = dm_scan_fdt_dev, |
| 89 | .probe = pmic_qcom_probe, |
| 90 | .ops = &pmic_qcom_ops, |
| 91 | .priv_auto = sizeof(struct pmic_qcom_priv), |
| 92 | }; |