1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * PWM Greybus driver. 4 * 5 * Copyright 2014 Google Inc. 6 * Copyright 2014 Linaro Ltd. 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/slab.h> 12 #include <linux/pwm.h> 13 #include <linux/greybus.h> 14 15 #include "gbphy.h" 16 17 struct gb_pwm_chip { 18 struct gb_connection *connection; 19 u8 pwm_max; /* max pwm number */ 20 21 struct pwm_chip chip; 22 struct pwm_chip *pwm; 23 }; 24 25 static inline struct gb_pwm_chip *pwm_chip_to_gb_pwm_chip(struct pwm_chip *chip) 26 { 27 return container_of(chip, struct gb_pwm_chip, chip); 28 } 29 30 static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc) 31 { 32 struct gb_pwm_count_response response; 33 int ret; 34 35 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT, 36 NULL, 0, &response, sizeof(response)); 37 if (ret) 38 return ret; 39 pwmc->pwm_max = response.count; 40 return 0; 41 } 42 43 static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc, 44 u8 which) 45 { 46 struct gb_pwm_activate_request request; 47 struct gbphy_device *gbphy_dev; 48 int ret; 49 50 if (which > pwmc->pwm_max) 51 return -EINVAL; 52 53 request.which = which; 54 55 gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 56 ret = gbphy_runtime_get_sync(gbphy_dev); 57 if (ret) 58 return ret; 59 60 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE, 61 &request, sizeof(request), NULL, 0); 62 63 gbphy_runtime_put_autosuspend(gbphy_dev); 64 65 return ret; 66 } 67 68 static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc, 69 u8 which) 70 { 71 struct gb_pwm_deactivate_request request; 72 struct gbphy_device *gbphy_dev; 73 int ret; 74 75 if (which > pwmc->pwm_max) 76 return -EINVAL; 77 78 request.which = which; 79 80 gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 81 ret = gbphy_runtime_get_sync(gbphy_dev); 82 if (ret) 83 return ret; 84 85 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE, 86 &request, sizeof(request), NULL, 0); 87 88 gbphy_runtime_put_autosuspend(gbphy_dev); 89 90 return ret; 91 } 92 93 static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc, 94 u8 which, u32 duty, u32 period) 95 { 96 struct gb_pwm_config_request request; 97 struct gbphy_device *gbphy_dev; 98 int ret; 99 100 if (which > pwmc->pwm_max) 101 return -EINVAL; 102 103 request.which = which; 104 request.duty = cpu_to_le32(duty); 105 request.period = cpu_to_le32(period); 106 107 gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 108 ret = gbphy_runtime_get_sync(gbphy_dev); 109 if (ret) 110 return ret; 111 112 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG, 113 &request, sizeof(request), NULL, 0); 114 115 gbphy_runtime_put_autosuspend(gbphy_dev); 116 117 return ret; 118 } 119 120 static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc, 121 u8 which, u8 polarity) 122 { 123 struct gb_pwm_polarity_request request; 124 struct gbphy_device *gbphy_dev; 125 int ret; 126 127 if (which > pwmc->pwm_max) 128 return -EINVAL; 129 130 request.which = which; 131 request.polarity = polarity; 132 133 gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 134 ret = gbphy_runtime_get_sync(gbphy_dev); 135 if (ret) 136 return ret; 137 138 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY, 139 &request, sizeof(request), NULL, 0); 140 141 gbphy_runtime_put_autosuspend(gbphy_dev); 142 143 return ret; 144 } 145 146 static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc, 147 u8 which) 148 { 149 struct gb_pwm_enable_request request; 150 struct gbphy_device *gbphy_dev; 151 int ret; 152 153 if (which > pwmc->pwm_max) 154 return -EINVAL; 155 156 request.which = which; 157 158 gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 159 ret = gbphy_runtime_get_sync(gbphy_dev); 160 if (ret) 161 return ret; 162 163 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE, 164 &request, sizeof(request), NULL, 0); 165 if (ret) 166 gbphy_runtime_put_autosuspend(gbphy_dev); 167 168 return ret; 169 } 170 171 static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc, 172 u8 which) 173 { 174 struct gb_pwm_disable_request request; 175 struct gbphy_device *gbphy_dev; 176 int ret; 177 178 if (which > pwmc->pwm_max) 179 return -EINVAL; 180 181 request.which = which; 182 183 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE, 184 &request, sizeof(request), NULL, 0); 185 186 gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 187 gbphy_runtime_put_autosuspend(gbphy_dev); 188 189 return ret; 190 } 191 192 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 193 { 194 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 195 196 return gb_pwm_activate_operation(pwmc, pwm->hwpwm); 197 }; 198 199 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) 200 { 201 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 202 203 if (pwm_is_enabled(pwm)) 204 dev_warn(chip->dev, "freeing PWM device without disabling\n"); 205 206 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm); 207 } 208 209 static int gb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 210 const struct pwm_state *state) 211 { 212 int err; 213 bool enabled = pwm->state.enabled; 214 u64 period = state->period; 215 u64 duty_cycle = state->duty_cycle; 216 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 217 218 /* Set polarity */ 219 if (state->polarity != pwm->state.polarity) { 220 if (enabled) { 221 gb_pwm_disable_operation(pwmc, pwm->hwpwm); 222 enabled = false; 223 } 224 err = gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, state->polarity); 225 if (err) 226 return err; 227 } 228 229 if (!state->enabled) { 230 if (enabled) 231 gb_pwm_disable_operation(pwmc, pwm->hwpwm); 232 return 0; 233 } 234 235 /* 236 * Set period and duty cycle 237 * 238 * PWM privodes 64-bit period and duty_cycle, but greybus only accepts 239 * 32-bit, so their values have to be limited to U32_MAX. 240 */ 241 if (period > U32_MAX) 242 period = U32_MAX; 243 244 if (duty_cycle > period) 245 duty_cycle = period; 246 247 err = gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_cycle, period); 248 if (err) 249 return err; 250 251 /* enable/disable */ 252 if (!enabled) 253 return gb_pwm_enable_operation(pwmc, pwm->hwpwm); 254 255 return 0; 256 } 257 258 static const struct pwm_ops gb_pwm_ops = { 259 .request = gb_pwm_request, 260 .free = gb_pwm_free, 261 .apply = gb_pwm_apply, 262 .owner = THIS_MODULE, 263 }; 264 265 static int gb_pwm_probe(struct gbphy_device *gbphy_dev, 266 const struct gbphy_device_id *id) 267 { 268 struct gb_connection *connection; 269 struct gb_pwm_chip *pwmc; 270 struct pwm_chip *pwm; 271 int ret; 272 273 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL); 274 if (!pwmc) 275 return -ENOMEM; 276 277 connection = gb_connection_create(gbphy_dev->bundle, 278 le16_to_cpu(gbphy_dev->cport_desc->id), 279 NULL); 280 if (IS_ERR(connection)) { 281 ret = PTR_ERR(connection); 282 goto exit_pwmc_free; 283 } 284 285 pwmc->connection = connection; 286 gb_connection_set_data(connection, pwmc); 287 gb_gbphy_set_data(gbphy_dev, pwmc); 288 289 ret = gb_connection_enable(connection); 290 if (ret) 291 goto exit_connection_destroy; 292 293 /* Query number of pwms present */ 294 ret = gb_pwm_count_operation(pwmc); 295 if (ret) 296 goto exit_connection_disable; 297 298 pwm = &pwmc->chip; 299 300 pwm->dev = &gbphy_dev->dev; 301 pwm->ops = &gb_pwm_ops; 302 pwm->npwm = pwmc->pwm_max + 1; 303 304 ret = pwmchip_add(pwm); 305 if (ret) { 306 dev_err(&gbphy_dev->dev, 307 "failed to register PWM: %d\n", ret); 308 goto exit_connection_disable; 309 } 310 311 gbphy_runtime_put_autosuspend(gbphy_dev); 312 return 0; 313 314 exit_connection_disable: 315 gb_connection_disable(connection); 316 exit_connection_destroy: 317 gb_connection_destroy(connection); 318 exit_pwmc_free: 319 kfree(pwmc); 320 return ret; 321 } 322 323 static void gb_pwm_remove(struct gbphy_device *gbphy_dev) 324 { 325 struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev); 326 struct gb_connection *connection = pwmc->connection; 327 int ret; 328 329 ret = gbphy_runtime_get_sync(gbphy_dev); 330 if (ret) 331 gbphy_runtime_get_noresume(gbphy_dev); 332 333 pwmchip_remove(&pwmc->chip); 334 gb_connection_disable(connection); 335 gb_connection_destroy(connection); 336 kfree(pwmc); 337 } 338 339 static const struct gbphy_device_id gb_pwm_id_table[] = { 340 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) }, 341 { }, 342 }; 343 MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table); 344 345 static struct gbphy_driver pwm_driver = { 346 .name = "pwm", 347 .probe = gb_pwm_probe, 348 .remove = gb_pwm_remove, 349 .id_table = gb_pwm_id_table, 350 }; 351 352 module_gbphy_driver(pwm_driver); 353 MODULE_LICENSE("GPL v2"); 354