Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * PWM Greybus driver. |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * Copyright 2014 Linaro Ltd. |
| 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/pwm.h> |
Alex Elder | e9385e5 | 2015-05-22 12:35:31 -0500 | [diff] [blame] | 14 | |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 15 | #include "greybus.h" |
| 16 | |
| 17 | struct gb_pwm_chip { |
| 18 | struct gb_connection *connection; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 19 | u8 pwm_max; /* max pwm number */ |
| 20 | |
| 21 | struct pwm_chip chip; |
| 22 | struct pwm_chip *pwm; |
| 23 | }; |
| 24 | #define pwm_chip_to_gb_pwm_chip(chip) \ |
| 25 | container_of(chip, struct gb_pwm_chip, chip) |
| 26 | |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 27 | |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 28 | static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc) |
| 29 | { |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 30 | struct gb_pwm_count_response response; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 31 | int ret; |
| 32 | |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 33 | ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT, |
| 34 | NULL, 0, &response, sizeof(response)); |
| 35 | if (ret) |
| 36 | return ret; |
| 37 | pwmc->pwm_max = response.count; |
| 38 | return 0; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc, |
| 42 | u8 which) |
| 43 | { |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 44 | struct gb_pwm_activate_request request; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 45 | |
| 46 | if (which > pwmc->pwm_max) |
| 47 | return -EINVAL; |
| 48 | |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 49 | request.which = which; |
| 50 | return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE, |
| 51 | &request, sizeof(request), NULL, 0); |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc, |
| 55 | u8 which) |
| 56 | { |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 57 | struct gb_pwm_deactivate_request request; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 58 | |
| 59 | if (which > pwmc->pwm_max) |
| 60 | return -EINVAL; |
| 61 | |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 62 | request.which = which; |
| 63 | return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE, |
| 64 | &request, sizeof(request), NULL, 0); |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc, |
| 68 | u8 which, u32 duty, u32 period) |
| 69 | { |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 70 | struct gb_pwm_config_request request; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 71 | |
| 72 | if (which > pwmc->pwm_max) |
| 73 | return -EINVAL; |
| 74 | |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 75 | request.which = which; |
Alex Elder | 583d233 | 2014-12-02 15:48:09 -0600 | [diff] [blame] | 76 | request.duty = cpu_to_le32(duty); |
| 77 | request.period = cpu_to_le32(period); |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 78 | return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG, |
| 79 | &request, sizeof(request), NULL, 0); |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc, |
| 84 | u8 which, u8 polarity) |
| 85 | { |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 86 | struct gb_pwm_polarity_request request; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 87 | |
| 88 | if (which > pwmc->pwm_max) |
| 89 | return -EINVAL; |
| 90 | |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 91 | request.which = which; |
| 92 | request.polarity = polarity; |
| 93 | return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY, |
| 94 | &request, sizeof(request), NULL, 0); |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc, |
| 98 | u8 which) |
| 99 | { |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 100 | struct gb_pwm_enable_request request; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 101 | |
| 102 | if (which > pwmc->pwm_max) |
| 103 | return -EINVAL; |
| 104 | |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 105 | request.which = which; |
| 106 | return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE, |
| 107 | &request, sizeof(request), NULL, 0); |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc, |
| 111 | u8 which) |
| 112 | { |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 113 | struct gb_pwm_disable_request request; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 114 | |
| 115 | if (which > pwmc->pwm_max) |
| 116 | return -EINVAL; |
| 117 | |
Greg Kroah-Hartman | bf2329f | 2014-11-23 17:45:24 -0800 | [diff] [blame] | 118 | request.which = which; |
| 119 | return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE, |
| 120 | &request, sizeof(request), NULL, 0); |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
| 124 | { |
| 125 | struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); |
| 126 | |
| 127 | return gb_pwm_activate_operation(pwmc, pwm->hwpwm); |
| 128 | }; |
| 129 | |
| 130 | static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 131 | { |
| 132 | struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); |
| 133 | |
| 134 | if (test_bit(PWMF_ENABLED, &pwm->flags)) |
| 135 | dev_warn(chip->dev, "freeing PWM device without disabling\n"); |
| 136 | |
| 137 | gb_pwm_deactivate_operation(pwmc, pwm->hwpwm); |
| 138 | } |
| 139 | |
| 140 | static int gb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
| 141 | int duty_ns, int period_ns) |
| 142 | { |
| 143 | struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); |
| 144 | |
| 145 | return gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_ns, period_ns); |
| 146 | }; |
| 147 | |
| 148 | static int gb_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm, |
| 149 | enum pwm_polarity polarity) |
| 150 | { |
| 151 | struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); |
| 152 | |
| 153 | return gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, polarity); |
| 154 | }; |
| 155 | |
| 156 | static int gb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 157 | { |
| 158 | struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); |
| 159 | |
| 160 | return gb_pwm_enable_operation(pwmc, pwm->hwpwm); |
| 161 | }; |
| 162 | |
| 163 | static void gb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 164 | { |
| 165 | struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); |
| 166 | |
| 167 | gb_pwm_disable_operation(pwmc, pwm->hwpwm); |
| 168 | }; |
| 169 | |
| 170 | static const struct pwm_ops gb_pwm_ops = { |
| 171 | .request = gb_pwm_request, |
| 172 | .free = gb_pwm_free, |
| 173 | .config = gb_pwm_config, |
| 174 | .set_polarity = gb_pwm_set_polarity, |
| 175 | .enable = gb_pwm_enable, |
| 176 | .disable = gb_pwm_disable, |
| 177 | .owner = THIS_MODULE, |
| 178 | }; |
| 179 | |
| 180 | static int gb_pwm_connection_init(struct gb_connection *connection) |
| 181 | { |
| 182 | struct gb_pwm_chip *pwmc; |
| 183 | struct pwm_chip *pwm; |
| 184 | int ret; |
| 185 | |
| 186 | pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL); |
| 187 | if (!pwmc) |
| 188 | return -ENOMEM; |
| 189 | pwmc->connection = connection; |
Alex Elder | 93bbe85 | 2014-12-03 12:27:42 -0600 | [diff] [blame] | 190 | connection->private = pwmc; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 191 | |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 192 | /* Query number of pwms present */ |
| 193 | ret = gb_pwm_count_operation(pwmc); |
| 194 | if (ret) |
| 195 | goto out_err; |
| 196 | |
| 197 | pwm = &pwmc->chip; |
| 198 | |
Greg Kroah-Hartman | 8d5732f | 2015-10-14 11:18:50 -0700 | [diff] [blame] | 199 | pwm->dev = &connection->bundle->dev; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 200 | pwm->ops = &gb_pwm_ops; |
| 201 | pwm->base = -1; /* Allocate base dynamically */ |
| 202 | pwm->npwm = pwmc->pwm_max + 1; |
| 203 | pwm->can_sleep = true; /* FIXME */ |
| 204 | |
| 205 | ret = pwmchip_add(pwm); |
| 206 | if (ret) { |
Greg Kroah-Hartman | 8d5732f | 2015-10-14 11:18:50 -0700 | [diff] [blame] | 207 | dev_err(&connection->bundle->dev, |
| 208 | "failed to register PWM: %d\n", ret); |
Johan Hovold | 6a80ed4 | 2015-03-17 10:55:50 +0100 | [diff] [blame] | 209 | goto out_err; |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 210 | } |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | out_err: |
| 214 | kfree(pwmc); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static void gb_pwm_connection_exit(struct gb_connection *connection) |
| 219 | { |
| 220 | struct gb_pwm_chip *pwmc = connection->private; |
| 221 | |
| 222 | if (!pwmc) |
| 223 | return; |
| 224 | |
| 225 | pwmchip_remove(&pwmc->chip); |
| 226 | /* kref_put(pwmc->connection) */ |
| 227 | kfree(pwmc); |
| 228 | } |
| 229 | |
| 230 | static struct gb_protocol pwm_protocol = { |
Greg Kroah-Hartman | 7422a1e | 2014-12-24 13:01:45 -0800 | [diff] [blame] | 231 | .name = "pwm", |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 232 | .id = GREYBUS_PROTOCOL_PWM, |
Viresh Kumar | 3fb97e4 | 2015-08-08 10:25:37 +0530 | [diff] [blame] | 233 | .major = GB_PWM_VERSION_MAJOR, |
| 234 | .minor = GB_PWM_VERSION_MINOR, |
Matt Porter | 34c6507 | 2014-11-13 09:14:13 -0500 | [diff] [blame] | 235 | .connection_init = gb_pwm_connection_init, |
| 236 | .connection_exit = gb_pwm_connection_exit, |
| 237 | .request_recv = NULL, /* no incoming requests */ |
| 238 | }; |
| 239 | |
Viresh Kumar | e18822e | 2015-07-01 12:13:52 +0530 | [diff] [blame] | 240 | gb_builtin_protocol_driver(pwm_protocol); |