Ram Chandrasekar | 3b48e5e | 2017-05-04 16:55:50 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/thermal.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/mailbox_client.h> |
| 19 | |
| 20 | #define REG_CDEV_DRIVER "reg-cooling-device" |
| 21 | #define REG_MSG_FORMAT "{class:volt_flr, event:zero_temp, res:%s, value:%s}" |
| 22 | #define REG_CDEV_MAX_STATE 1 |
| 23 | #define MBOX_TOUT_MS 1000 |
| 24 | #define REG_MSG_MAX_LEN 100 |
| 25 | |
| 26 | struct reg_cooling_device { |
| 27 | struct thermal_cooling_device *cdev; |
| 28 | unsigned int min_state; |
| 29 | const char *resource_name; |
| 30 | struct mbox_chan *qmp_chan; |
| 31 | struct mbox_client *client; |
| 32 | }; |
| 33 | |
| 34 | struct aop_msg { |
| 35 | uint32_t len; |
| 36 | void *msg; |
| 37 | }; |
| 38 | |
| 39 | enum regulator_rail_type { |
| 40 | REG_COOLING_CX, |
| 41 | REG_COOLING_MX, |
| 42 | REG_COOLING_EBI, |
| 43 | REG_COOLING_NR, |
| 44 | }; |
| 45 | |
| 46 | static char *regulator_rail[REG_COOLING_NR] = { |
| 47 | "cx", |
| 48 | "mx", |
| 49 | "ebi", |
| 50 | }; |
| 51 | |
| 52 | static int aop_send_msg(struct reg_cooling_device *reg_dev, int min_state) |
| 53 | { |
| 54 | char msg_buf[REG_MSG_MAX_LEN] = {0}; |
| 55 | int ret = 0; |
| 56 | struct aop_msg msg; |
| 57 | |
| 58 | if (!reg_dev->qmp_chan) { |
| 59 | pr_err("mbox not initialized for resource:%s\n", |
| 60 | reg_dev->resource_name); |
| 61 | return -EINVAL; |
| 62 | } |
| 63 | |
| 64 | ret = snprintf(msg_buf, REG_MSG_MAX_LEN, REG_MSG_FORMAT, |
| 65 | reg_dev->resource_name, |
| 66 | (min_state == REG_CDEV_MAX_STATE) ? "off" : "on"); |
| 67 | if (ret >= REG_MSG_MAX_LEN) { |
| 68 | pr_err("Message too long for resource:%s\n", |
| 69 | reg_dev->resource_name); |
| 70 | return -E2BIG; |
| 71 | } |
| 72 | msg.len = REG_MSG_MAX_LEN; |
| 73 | msg.msg = msg_buf; |
| 74 | ret = mbox_send_message(reg_dev->qmp_chan, &msg); |
| 75 | |
| 76 | return (ret < 0) ? ret : 0; |
| 77 | } |
| 78 | |
| 79 | static int reg_get_max_state(struct thermal_cooling_device *cdev, |
| 80 | unsigned long *state) |
| 81 | { |
| 82 | *state = REG_CDEV_MAX_STATE; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int reg_get_min_state(struct thermal_cooling_device *cdev, |
| 87 | unsigned long *state) |
| 88 | { |
| 89 | struct reg_cooling_device *reg_dev = cdev->devdata; |
| 90 | |
| 91 | *state = reg_dev->min_state; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int reg_send_min_state(struct thermal_cooling_device *cdev, |
| 96 | unsigned long state) |
| 97 | { |
| 98 | struct reg_cooling_device *reg_dev = cdev->devdata; |
| 99 | int ret = 0; |
| 100 | |
| 101 | if (state > REG_CDEV_MAX_STATE) |
| 102 | state = REG_CDEV_MAX_STATE; |
| 103 | |
| 104 | if (reg_dev->min_state == state) |
| 105 | return ret; |
| 106 | |
| 107 | ret = aop_send_msg(reg_dev, state); |
| 108 | if (ret) { |
| 109 | pr_err("regulator:%s switching to floor %lu error. err:%d\n", |
| 110 | reg_dev->resource_name, state, ret); |
| 111 | } else { |
| 112 | pr_debug("regulator:%s switched to %lu from %d\n", |
| 113 | reg_dev->resource_name, state, reg_dev->min_state); |
| 114 | reg_dev->min_state = state; |
| 115 | } |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int reg_get_cur_state(struct thermal_cooling_device *cdev, |
| 121 | unsigned long *state) |
| 122 | { |
| 123 | *state = 0; |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int reg_send_cur_state(struct thermal_cooling_device *cdev, |
| 128 | unsigned long state) |
| 129 | { |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static struct thermal_cooling_device_ops reg_dev_ops = { |
| 134 | .get_max_state = reg_get_max_state, |
| 135 | .get_cur_state = reg_get_cur_state, |
| 136 | .set_cur_state = reg_send_cur_state, |
| 137 | .set_min_state = reg_send_min_state, |
| 138 | .get_min_state = reg_get_min_state, |
| 139 | }; |
| 140 | |
| 141 | static int reg_init_mbox(struct platform_device *pdev, |
| 142 | struct reg_cooling_device *reg_dev) |
| 143 | { |
| 144 | reg_dev->client = devm_kzalloc(&pdev->dev, sizeof(*reg_dev->client), |
| 145 | GFP_KERNEL); |
| 146 | if (!reg_dev->client) |
| 147 | return -ENOMEM; |
| 148 | |
| 149 | reg_dev->client->dev = &pdev->dev; |
| 150 | reg_dev->client->tx_block = true; |
| 151 | reg_dev->client->tx_tout = MBOX_TOUT_MS; |
| 152 | reg_dev->client->knows_txdone = false; |
| 153 | |
| 154 | reg_dev->qmp_chan = mbox_request_channel(reg_dev->client, 0); |
| 155 | if (IS_ERR(reg_dev->qmp_chan)) { |
| 156 | dev_err(&pdev->dev, "Mbox request failed. err:%ld\n", |
| 157 | PTR_ERR(reg_dev->qmp_chan)); |
| 158 | return PTR_ERR(reg_dev->qmp_chan); |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int reg_dev_probe(struct platform_device *pdev) |
| 165 | { |
| 166 | int ret = 0, idx = 0; |
| 167 | struct reg_cooling_device *reg_dev = NULL; |
| 168 | |
| 169 | reg_dev = devm_kzalloc(&pdev->dev, sizeof(*reg_dev), GFP_KERNEL); |
| 170 | if (!reg_dev) |
| 171 | return -ENOMEM; |
| 172 | |
| 173 | ret = reg_init_mbox(pdev, reg_dev); |
| 174 | if (ret) |
| 175 | return ret; |
| 176 | |
| 177 | ret = of_property_read_string(pdev->dev.of_node, |
| 178 | "qcom,reg-resource-name", |
| 179 | ®_dev->resource_name); |
| 180 | if (ret) { |
| 181 | dev_err(&pdev->dev, "Error reading resource name. err:%d\n", |
| 182 | ret); |
| 183 | goto mbox_free; |
| 184 | } |
| 185 | |
| 186 | for (idx = 0; idx < REG_COOLING_NR; idx++) { |
| 187 | if (!strcmp(reg_dev->resource_name, regulator_rail[idx])) |
| 188 | break; |
| 189 | } |
| 190 | if (idx == REG_COOLING_NR) { |
| 191 | dev_err(&pdev->dev, "Invalid regulator resource name:%s\n", |
| 192 | reg_dev->resource_name); |
| 193 | ret = -EINVAL; |
| 194 | goto mbox_free; |
| 195 | } |
| 196 | reg_dev->min_state = REG_CDEV_MAX_STATE; |
| 197 | reg_dev->cdev = thermal_of_cooling_device_register( |
| 198 | pdev->dev.of_node, |
| 199 | (char *)reg_dev->resource_name, |
| 200 | reg_dev, ®_dev_ops); |
| 201 | if (IS_ERR(reg_dev->cdev)) |
| 202 | goto mbox_free; |
| 203 | |
| 204 | return ret; |
| 205 | |
| 206 | mbox_free: |
| 207 | mbox_free_channel(reg_dev->qmp_chan); |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static const struct of_device_id reg_dev_of_match[] = { |
| 213 | {.compatible = "qcom,rpmh-reg-cdev", }, |
| 214 | {} |
| 215 | }; |
| 216 | |
| 217 | static struct platform_driver reg_dev_driver = { |
| 218 | .driver = { |
| 219 | .name = REG_CDEV_DRIVER, |
| 220 | .of_match_table = reg_dev_of_match, |
| 221 | }, |
| 222 | .probe = reg_dev_probe, |
| 223 | }; |
| 224 | builtin_platform_driver(reg_dev_driver); |