| .. | .. |
|---|
| 2 | 2 | /* |
|---|
| 3 | 3 | * System Control and Management Interface (SCMI) Clock Protocol |
|---|
| 4 | 4 | * |
|---|
| 5 | | - * Copyright (C) 2018 ARM Ltd. |
|---|
| 5 | + * Copyright (C) 2018-2020 ARM Ltd. |
|---|
| 6 | 6 | */ |
|---|
| 7 | + |
|---|
| 8 | +#include <linux/module.h> |
|---|
| 9 | +#include <linux/sort.h> |
|---|
| 7 | 10 | |
|---|
| 8 | 11 | #include "common.h" |
|---|
| 9 | 12 | |
|---|
| .. | .. |
|---|
| 72 | 75 | struct scmi_clock_info *clk; |
|---|
| 73 | 76 | }; |
|---|
| 74 | 77 | |
|---|
| 75 | | -static int scmi_clock_protocol_attributes_get(const struct scmi_handle *handle, |
|---|
| 76 | | - struct clock_info *ci) |
|---|
| 78 | +static int |
|---|
| 79 | +scmi_clock_protocol_attributes_get(const struct scmi_protocol_handle *ph, |
|---|
| 80 | + struct clock_info *ci) |
|---|
| 77 | 81 | { |
|---|
| 78 | 82 | int ret; |
|---|
| 79 | 83 | struct scmi_xfer *t; |
|---|
| 80 | 84 | struct scmi_msg_resp_clock_protocol_attributes *attr; |
|---|
| 81 | 85 | |
|---|
| 82 | | - ret = scmi_xfer_get_init(handle, PROTOCOL_ATTRIBUTES, |
|---|
| 83 | | - SCMI_PROTOCOL_CLOCK, 0, sizeof(*attr), &t); |
|---|
| 86 | + ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, |
|---|
| 87 | + 0, sizeof(*attr), &t); |
|---|
| 84 | 88 | if (ret) |
|---|
| 85 | 89 | return ret; |
|---|
| 86 | 90 | |
|---|
| 87 | 91 | attr = t->rx.buf; |
|---|
| 88 | 92 | |
|---|
| 89 | | - ret = scmi_do_xfer(handle, t); |
|---|
| 93 | + ret = ph->xops->do_xfer(ph, t); |
|---|
| 90 | 94 | if (!ret) { |
|---|
| 91 | 95 | ci->num_clocks = le16_to_cpu(attr->num_clocks); |
|---|
| 92 | 96 | ci->max_async_req = attr->max_async_req; |
|---|
| 93 | 97 | } |
|---|
| 94 | 98 | |
|---|
| 95 | | - scmi_xfer_put(handle, t); |
|---|
| 99 | + ph->xops->xfer_put(ph, t); |
|---|
| 96 | 100 | return ret; |
|---|
| 97 | 101 | } |
|---|
| 98 | 102 | |
|---|
| 99 | | -static int scmi_clock_attributes_get(const struct scmi_handle *handle, |
|---|
| 103 | +static int scmi_clock_attributes_get(const struct scmi_protocol_handle *ph, |
|---|
| 100 | 104 | u32 clk_id, struct scmi_clock_info *clk) |
|---|
| 101 | 105 | { |
|---|
| 102 | 106 | int ret; |
|---|
| 103 | 107 | struct scmi_xfer *t; |
|---|
| 104 | 108 | struct scmi_msg_resp_clock_attributes *attr; |
|---|
| 105 | 109 | |
|---|
| 106 | | - ret = scmi_xfer_get_init(handle, CLOCK_ATTRIBUTES, SCMI_PROTOCOL_CLOCK, |
|---|
| 107 | | - sizeof(clk_id), sizeof(*attr), &t); |
|---|
| 110 | + ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES, |
|---|
| 111 | + sizeof(clk_id), sizeof(*attr), &t); |
|---|
| 108 | 112 | if (ret) |
|---|
| 109 | 113 | return ret; |
|---|
| 110 | 114 | |
|---|
| 111 | 115 | put_unaligned_le32(clk_id, t->tx.buf); |
|---|
| 112 | 116 | attr = t->rx.buf; |
|---|
| 113 | 117 | |
|---|
| 114 | | - ret = scmi_do_xfer(handle, t); |
|---|
| 118 | + ret = ph->xops->do_xfer(ph, t); |
|---|
| 115 | 119 | if (!ret) |
|---|
| 116 | 120 | strlcpy(clk->name, attr->name, SCMI_MAX_STR_SIZE); |
|---|
| 117 | 121 | else |
|---|
| 118 | 122 | clk->name[0] = '\0'; |
|---|
| 119 | 123 | |
|---|
| 120 | | - scmi_xfer_put(handle, t); |
|---|
| 124 | + ph->xops->xfer_put(ph, t); |
|---|
| 121 | 125 | return ret; |
|---|
| 122 | 126 | } |
|---|
| 123 | 127 | |
|---|
| 128 | +static int rate_cmp_func(const void *_r1, const void *_r2) |
|---|
| 129 | +{ |
|---|
| 130 | + const u64 *r1 = _r1, *r2 = _r2; |
|---|
| 131 | + |
|---|
| 132 | + if (*r1 < *r2) |
|---|
| 133 | + return -1; |
|---|
| 134 | + else if (*r1 == *r2) |
|---|
| 135 | + return 0; |
|---|
| 136 | + else |
|---|
| 137 | + return 1; |
|---|
| 138 | +} |
|---|
| 139 | + |
|---|
| 124 | 140 | static int |
|---|
| 125 | | -scmi_clock_describe_rates_get(const struct scmi_handle *handle, u32 clk_id, |
|---|
| 141 | +scmi_clock_describe_rates_get(const struct scmi_protocol_handle *ph, u32 clk_id, |
|---|
| 126 | 142 | struct scmi_clock_info *clk) |
|---|
| 127 | 143 | { |
|---|
| 128 | | - u64 *rate; |
|---|
| 144 | + u64 *rate = NULL; |
|---|
| 129 | 145 | int ret, cnt; |
|---|
| 130 | 146 | bool rate_discrete = false; |
|---|
| 131 | 147 | u32 tot_rate_cnt = 0, rates_flag; |
|---|
| .. | .. |
|---|
| 134 | 150 | struct scmi_msg_clock_describe_rates *clk_desc; |
|---|
| 135 | 151 | struct scmi_msg_resp_clock_describe_rates *rlist; |
|---|
| 136 | 152 | |
|---|
| 137 | | - ret = scmi_xfer_get_init(handle, CLOCK_DESCRIBE_RATES, |
|---|
| 138 | | - SCMI_PROTOCOL_CLOCK, sizeof(*clk_desc), 0, &t); |
|---|
| 153 | + ret = ph->xops->xfer_get_init(ph, CLOCK_DESCRIBE_RATES, |
|---|
| 154 | + sizeof(*clk_desc), 0, &t); |
|---|
| 139 | 155 | if (ret) |
|---|
| 140 | 156 | return ret; |
|---|
| 141 | 157 | |
|---|
| .. | .. |
|---|
| 147 | 163 | /* Set the number of rates to be skipped/already read */ |
|---|
| 148 | 164 | clk_desc->rate_index = cpu_to_le32(tot_rate_cnt); |
|---|
| 149 | 165 | |
|---|
| 150 | | - ret = scmi_do_xfer(handle, t); |
|---|
| 166 | + ret = ph->xops->do_xfer(ph, t); |
|---|
| 151 | 167 | if (ret) |
|---|
| 152 | 168 | goto err; |
|---|
| 153 | 169 | |
|---|
| .. | .. |
|---|
| 157 | 173 | num_returned = NUM_RETURNED(rates_flag); |
|---|
| 158 | 174 | |
|---|
| 159 | 175 | if (tot_rate_cnt + num_returned > SCMI_MAX_NUM_RATES) { |
|---|
| 160 | | - dev_err(handle->dev, "No. of rates > MAX_NUM_RATES"); |
|---|
| 176 | + dev_err(ph->dev, "No. of rates > MAX_NUM_RATES"); |
|---|
| 161 | 177 | break; |
|---|
| 162 | 178 | } |
|---|
| 163 | 179 | |
|---|
| .. | .. |
|---|
| 165 | 181 | clk->range.min_rate = RATE_TO_U64(rlist->rate[0]); |
|---|
| 166 | 182 | clk->range.max_rate = RATE_TO_U64(rlist->rate[1]); |
|---|
| 167 | 183 | clk->range.step_size = RATE_TO_U64(rlist->rate[2]); |
|---|
| 168 | | - dev_dbg(handle->dev, "Min %llu Max %llu Step %llu Hz\n", |
|---|
| 184 | + dev_dbg(ph->dev, "Min %llu Max %llu Step %llu Hz\n", |
|---|
| 169 | 185 | clk->range.min_rate, clk->range.max_rate, |
|---|
| 170 | 186 | clk->range.step_size); |
|---|
| 171 | 187 | break; |
|---|
| .. | .. |
|---|
| 174 | 190 | rate = &clk->list.rates[tot_rate_cnt]; |
|---|
| 175 | 191 | for (cnt = 0; cnt < num_returned; cnt++, rate++) { |
|---|
| 176 | 192 | *rate = RATE_TO_U64(rlist->rate[cnt]); |
|---|
| 177 | | - dev_dbg(handle->dev, "Rate %llu Hz\n", *rate); |
|---|
| 193 | + dev_dbg(ph->dev, "Rate %llu Hz\n", *rate); |
|---|
| 178 | 194 | } |
|---|
| 179 | 195 | |
|---|
| 180 | 196 | tot_rate_cnt += num_returned; |
|---|
| 197 | + |
|---|
| 198 | + ph->xops->reset_rx_to_maxsz(ph, t); |
|---|
| 181 | 199 | /* |
|---|
| 182 | 200 | * check for both returned and remaining to avoid infinite |
|---|
| 183 | 201 | * loop due to buggy firmware |
|---|
| 184 | 202 | */ |
|---|
| 185 | 203 | } while (num_returned && num_remaining); |
|---|
| 186 | 204 | |
|---|
| 187 | | - if (rate_discrete) |
|---|
| 205 | + if (rate_discrete && rate) { |
|---|
| 188 | 206 | clk->list.num_rates = tot_rate_cnt; |
|---|
| 207 | + sort(clk->list.rates, tot_rate_cnt, sizeof(*rate), |
|---|
| 208 | + rate_cmp_func, NULL); |
|---|
| 209 | + } |
|---|
| 189 | 210 | |
|---|
| 190 | 211 | clk->rate_discrete = rate_discrete; |
|---|
| 191 | 212 | |
|---|
| 192 | 213 | err: |
|---|
| 193 | | - scmi_xfer_put(handle, t); |
|---|
| 214 | + ph->xops->xfer_put(ph, t); |
|---|
| 194 | 215 | return ret; |
|---|
| 195 | 216 | } |
|---|
| 196 | 217 | |
|---|
| 197 | 218 | static int |
|---|
| 198 | | -scmi_clock_rate_get(const struct scmi_handle *handle, u32 clk_id, u64 *value) |
|---|
| 219 | +scmi_clock_rate_get(const struct scmi_protocol_handle *ph, |
|---|
| 220 | + u32 clk_id, u64 *value) |
|---|
| 199 | 221 | { |
|---|
| 200 | 222 | int ret; |
|---|
| 201 | 223 | struct scmi_xfer *t; |
|---|
| 202 | 224 | |
|---|
| 203 | | - ret = scmi_xfer_get_init(handle, CLOCK_RATE_GET, SCMI_PROTOCOL_CLOCK, |
|---|
| 204 | | - sizeof(__le32), sizeof(u64), &t); |
|---|
| 225 | + ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_GET, |
|---|
| 226 | + sizeof(__le32), sizeof(u64), &t); |
|---|
| 205 | 227 | if (ret) |
|---|
| 206 | 228 | return ret; |
|---|
| 207 | 229 | |
|---|
| 208 | 230 | put_unaligned_le32(clk_id, t->tx.buf); |
|---|
| 209 | 231 | |
|---|
| 210 | | - ret = scmi_do_xfer(handle, t); |
|---|
| 232 | + ret = ph->xops->do_xfer(ph, t); |
|---|
| 211 | 233 | if (!ret) |
|---|
| 212 | 234 | *value = get_unaligned_le64(t->rx.buf); |
|---|
| 213 | 235 | |
|---|
| 214 | | - scmi_xfer_put(handle, t); |
|---|
| 236 | + ph->xops->xfer_put(ph, t); |
|---|
| 215 | 237 | return ret; |
|---|
| 216 | 238 | } |
|---|
| 217 | 239 | |
|---|
| 218 | | -static int scmi_clock_rate_set(const struct scmi_handle *handle, u32 clk_id, |
|---|
| 219 | | - u64 rate) |
|---|
| 240 | +static int scmi_clock_rate_set(const struct scmi_protocol_handle *ph, |
|---|
| 241 | + u32 clk_id, u64 rate) |
|---|
| 220 | 242 | { |
|---|
| 221 | 243 | int ret; |
|---|
| 222 | 244 | u32 flags = 0; |
|---|
| 223 | 245 | struct scmi_xfer *t; |
|---|
| 224 | 246 | struct scmi_clock_set_rate *cfg; |
|---|
| 225 | | - struct clock_info *ci = handle->clk_priv; |
|---|
| 247 | + struct clock_info *ci = ph->get_priv(ph); |
|---|
| 226 | 248 | |
|---|
| 227 | | - ret = scmi_xfer_get_init(handle, CLOCK_RATE_SET, SCMI_PROTOCOL_CLOCK, |
|---|
| 228 | | - sizeof(*cfg), 0, &t); |
|---|
| 249 | + ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_SET, sizeof(*cfg), 0, &t); |
|---|
| 229 | 250 | if (ret) |
|---|
| 230 | 251 | return ret; |
|---|
| 231 | 252 | |
|---|
| .. | .. |
|---|
| 240 | 261 | cfg->value_high = cpu_to_le32(rate >> 32); |
|---|
| 241 | 262 | |
|---|
| 242 | 263 | if (flags & CLOCK_SET_ASYNC) |
|---|
| 243 | | - ret = scmi_do_xfer_with_response(handle, t); |
|---|
| 264 | + ret = ph->xops->do_xfer_with_response(ph, t); |
|---|
| 244 | 265 | else |
|---|
| 245 | | - ret = scmi_do_xfer(handle, t); |
|---|
| 266 | + ret = ph->xops->do_xfer(ph, t); |
|---|
| 246 | 267 | |
|---|
| 247 | 268 | if (ci->max_async_req) |
|---|
| 248 | 269 | atomic_dec(&ci->cur_async_req); |
|---|
| 249 | 270 | |
|---|
| 250 | | - scmi_xfer_put(handle, t); |
|---|
| 271 | + ph->xops->xfer_put(ph, t); |
|---|
| 251 | 272 | return ret; |
|---|
| 252 | 273 | } |
|---|
| 253 | 274 | |
|---|
| 254 | 275 | static int |
|---|
| 255 | | -scmi_clock_config_set(const struct scmi_handle *handle, u32 clk_id, u32 config) |
|---|
| 276 | +scmi_clock_config_set(const struct scmi_protocol_handle *ph, u32 clk_id, |
|---|
| 277 | + u32 config) |
|---|
| 256 | 278 | { |
|---|
| 257 | 279 | int ret; |
|---|
| 258 | 280 | struct scmi_xfer *t; |
|---|
| 259 | 281 | struct scmi_clock_set_config *cfg; |
|---|
| 260 | 282 | |
|---|
| 261 | | - ret = scmi_xfer_get_init(handle, CLOCK_CONFIG_SET, SCMI_PROTOCOL_CLOCK, |
|---|
| 262 | | - sizeof(*cfg), 0, &t); |
|---|
| 283 | + ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET, |
|---|
| 284 | + sizeof(*cfg), 0, &t); |
|---|
| 263 | 285 | if (ret) |
|---|
| 264 | 286 | return ret; |
|---|
| 265 | 287 | |
|---|
| .. | .. |
|---|
| 267 | 289 | cfg->id = cpu_to_le32(clk_id); |
|---|
| 268 | 290 | cfg->attributes = cpu_to_le32(config); |
|---|
| 269 | 291 | |
|---|
| 270 | | - ret = scmi_do_xfer(handle, t); |
|---|
| 292 | + ret = ph->xops->do_xfer(ph, t); |
|---|
| 271 | 293 | |
|---|
| 272 | | - scmi_xfer_put(handle, t); |
|---|
| 294 | + ph->xops->xfer_put(ph, t); |
|---|
| 273 | 295 | return ret; |
|---|
| 274 | 296 | } |
|---|
| 275 | 297 | |
|---|
| 276 | | -static int scmi_clock_enable(const struct scmi_handle *handle, u32 clk_id) |
|---|
| 298 | +static int scmi_clock_enable(const struct scmi_protocol_handle *ph, u32 clk_id) |
|---|
| 277 | 299 | { |
|---|
| 278 | | - return scmi_clock_config_set(handle, clk_id, CLOCK_ENABLE); |
|---|
| 300 | + return scmi_clock_config_set(ph, clk_id, CLOCK_ENABLE); |
|---|
| 279 | 301 | } |
|---|
| 280 | 302 | |
|---|
| 281 | | -static int scmi_clock_disable(const struct scmi_handle *handle, u32 clk_id) |
|---|
| 303 | +static int scmi_clock_disable(const struct scmi_protocol_handle *ph, u32 clk_id) |
|---|
| 282 | 304 | { |
|---|
| 283 | | - return scmi_clock_config_set(handle, clk_id, 0); |
|---|
| 305 | + return scmi_clock_config_set(ph, clk_id, 0); |
|---|
| 284 | 306 | } |
|---|
| 285 | 307 | |
|---|
| 286 | | -static int scmi_clock_count_get(const struct scmi_handle *handle) |
|---|
| 308 | +static int scmi_clock_count_get(const struct scmi_protocol_handle *ph) |
|---|
| 287 | 309 | { |
|---|
| 288 | | - struct clock_info *ci = handle->clk_priv; |
|---|
| 310 | + struct clock_info *ci = ph->get_priv(ph); |
|---|
| 289 | 311 | |
|---|
| 290 | 312 | return ci->num_clocks; |
|---|
| 291 | 313 | } |
|---|
| 292 | 314 | |
|---|
| 293 | 315 | static const struct scmi_clock_info * |
|---|
| 294 | | -scmi_clock_info_get(const struct scmi_handle *handle, u32 clk_id) |
|---|
| 316 | +scmi_clock_info_get(const struct scmi_protocol_handle *ph, u32 clk_id) |
|---|
| 295 | 317 | { |
|---|
| 296 | | - struct clock_info *ci = handle->clk_priv; |
|---|
| 318 | + struct clock_info *ci = ph->get_priv(ph); |
|---|
| 297 | 319 | struct scmi_clock_info *clk = ci->clk + clk_id; |
|---|
| 298 | 320 | |
|---|
| 299 | 321 | if (!clk->name[0]) |
|---|
| .. | .. |
|---|
| 302 | 324 | return clk; |
|---|
| 303 | 325 | } |
|---|
| 304 | 326 | |
|---|
| 305 | | -static struct scmi_clk_ops clk_ops = { |
|---|
| 327 | +static const struct scmi_clk_proto_ops clk_proto_ops = { |
|---|
| 306 | 328 | .count_get = scmi_clock_count_get, |
|---|
| 307 | 329 | .info_get = scmi_clock_info_get, |
|---|
| 308 | 330 | .rate_get = scmi_clock_rate_get, |
|---|
| .. | .. |
|---|
| 311 | 333 | .disable = scmi_clock_disable, |
|---|
| 312 | 334 | }; |
|---|
| 313 | 335 | |
|---|
| 314 | | -static int scmi_clock_protocol_init(struct scmi_handle *handle) |
|---|
| 336 | +static int scmi_clock_protocol_init(const struct scmi_protocol_handle *ph) |
|---|
| 315 | 337 | { |
|---|
| 316 | 338 | u32 version; |
|---|
| 317 | 339 | int clkid, ret; |
|---|
| 318 | 340 | struct clock_info *cinfo; |
|---|
| 319 | 341 | |
|---|
| 320 | | - scmi_version_get(handle, SCMI_PROTOCOL_CLOCK, &version); |
|---|
| 342 | + ph->xops->version_get(ph, &version); |
|---|
| 321 | 343 | |
|---|
| 322 | | - dev_dbg(handle->dev, "Clock Version %d.%d\n", |
|---|
| 344 | + dev_dbg(ph->dev, "Clock Version %d.%d\n", |
|---|
| 323 | 345 | PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version)); |
|---|
| 324 | 346 | |
|---|
| 325 | | - cinfo = devm_kzalloc(handle->dev, sizeof(*cinfo), GFP_KERNEL); |
|---|
| 347 | + cinfo = devm_kzalloc(ph->dev, sizeof(*cinfo), GFP_KERNEL); |
|---|
| 326 | 348 | if (!cinfo) |
|---|
| 327 | 349 | return -ENOMEM; |
|---|
| 328 | 350 | |
|---|
| 329 | | - scmi_clock_protocol_attributes_get(handle, cinfo); |
|---|
| 351 | + scmi_clock_protocol_attributes_get(ph, cinfo); |
|---|
| 330 | 352 | |
|---|
| 331 | | - cinfo->clk = devm_kcalloc(handle->dev, cinfo->num_clocks, |
|---|
| 353 | + cinfo->clk = devm_kcalloc(ph->dev, cinfo->num_clocks, |
|---|
| 332 | 354 | sizeof(*cinfo->clk), GFP_KERNEL); |
|---|
| 333 | 355 | if (!cinfo->clk) |
|---|
| 334 | 356 | return -ENOMEM; |
|---|
| .. | .. |
|---|
| 336 | 358 | for (clkid = 0; clkid < cinfo->num_clocks; clkid++) { |
|---|
| 337 | 359 | struct scmi_clock_info *clk = cinfo->clk + clkid; |
|---|
| 338 | 360 | |
|---|
| 339 | | - ret = scmi_clock_attributes_get(handle, clkid, clk); |
|---|
| 361 | + ret = scmi_clock_attributes_get(ph, clkid, clk); |
|---|
| 340 | 362 | if (!ret) |
|---|
| 341 | | - scmi_clock_describe_rates_get(handle, clkid, clk); |
|---|
| 363 | + scmi_clock_describe_rates_get(ph, clkid, clk); |
|---|
| 342 | 364 | } |
|---|
| 343 | 365 | |
|---|
| 344 | 366 | cinfo->version = version; |
|---|
| 345 | | - handle->clk_ops = &clk_ops; |
|---|
| 346 | | - handle->clk_priv = cinfo; |
|---|
| 347 | | - |
|---|
| 348 | | - return 0; |
|---|
| 367 | + return ph->set_priv(ph, cinfo); |
|---|
| 349 | 368 | } |
|---|
| 350 | 369 | |
|---|
| 351 | | -DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(SCMI_PROTOCOL_CLOCK, clock) |
|---|
| 370 | +static const struct scmi_protocol scmi_clock = { |
|---|
| 371 | + .id = SCMI_PROTOCOL_CLOCK, |
|---|
| 372 | + .owner = THIS_MODULE, |
|---|
| 373 | + .init_instance = &scmi_clock_protocol_init, |
|---|
| 374 | + .ops = &clk_proto_ops, |
|---|
| 375 | +}; |
|---|
| 376 | + |
|---|
| 377 | +DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(clock, scmi_clock) |
|---|