Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 1 | /* |
Rui Miguel Silva | 2724be0 | 2015-11-12 15:36:00 +0000 | [diff] [blame] | 2 | * Power Supply driver for a Greybus module. |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 3 | * |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 4 | * Copyright 2014-2015 Google Inc. |
| 5 | * Copyright 2014-2015 Linaro Ltd. |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 12 | #include <linux/power_supply.h> |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 15 | #include "greybus.h" |
| 16 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 17 | #define PROP_MAX 32 |
Greg Kroah-Hartman | 2bb7eae | 2014-10-20 15:24:57 +0800 | [diff] [blame] | 18 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 19 | struct gb_power_supply_prop { |
| 20 | enum power_supply_property prop; |
| 21 | u32 val; |
| 22 | u32 previous_val; |
| 23 | bool is_writeable; |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 24 | }; |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 25 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 26 | struct gb_power_supply { |
| 27 | u8 id; |
Rui Miguel Silva | ff85f72 | 2016-01-08 13:53:43 +0000 | [diff] [blame] | 28 | bool registered; |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 29 | #ifdef DRIVER_OWNS_PSY_STRUCT |
| 30 | struct power_supply psy; |
| 31 | #define to_gb_power_supply(x) container_of(x, struct gb_power_supply, psy) |
| 32 | #else |
| 33 | struct power_supply *psy; |
| 34 | struct power_supply_desc desc; |
| 35 | #define to_gb_power_supply(x) power_supply_get_drvdata(x) |
| 36 | #endif |
| 37 | char name[64]; |
| 38 | struct gb_power_supplies *supplies; |
| 39 | struct delayed_work work; |
| 40 | char *manufacturer; |
| 41 | char *model_name; |
| 42 | char *serial_number; |
| 43 | u8 type; |
| 44 | u8 properties_count; |
| 45 | u8 properties_count_str; |
| 46 | unsigned long last_update; |
| 47 | unsigned int update_interval; |
| 48 | bool changed; |
| 49 | struct gb_power_supply_prop *props; |
| 50 | enum power_supply_property *props_raw; |
| 51 | }; |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 52 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 53 | struct gb_power_supplies { |
| 54 | struct gb_connection *connection; |
| 55 | u8 supplies_count; |
| 56 | struct gb_power_supply *supply; |
| 57 | struct mutex supplies_lock; |
| 58 | }; |
| 59 | |
| 60 | /* cache time in milliseconds, if cache_time is set to 0 cache is disable */ |
| 61 | static unsigned int cache_time = 1000; |
| 62 | /* |
| 63 | * update interval initial and maximum value, between the two will |
| 64 | * back-off exponential |
| 65 | */ |
| 66 | static unsigned int update_interval_init = 1 * HZ; |
| 67 | static unsigned int update_interval_max = 30 * HZ; |
| 68 | |
| 69 | struct gb_power_supply_changes { |
| 70 | enum power_supply_property prop; |
| 71 | u32 tolerance_change; |
| 72 | }; |
| 73 | |
| 74 | static const struct gb_power_supply_changes psy_props_changes[] = { |
| 75 | { .prop = GB_POWER_SUPPLY_PROP_STATUS, |
| 76 | .tolerance_change = 0, |
| 77 | }, |
| 78 | { .prop = GB_POWER_SUPPLY_PROP_TEMP, |
| 79 | .tolerance_change = 500, |
| 80 | }, |
| 81 | { .prop = GB_POWER_SUPPLY_PROP_ONLINE, |
| 82 | .tolerance_change = 0, |
| 83 | }, |
| 84 | }; |
| 85 | |
| 86 | static struct gb_connection *get_conn_from_psy(struct gb_power_supply *gbpsy) |
| 87 | { |
| 88 | return gbpsy->supplies->connection; |
| 89 | } |
| 90 | |
| 91 | static struct gb_power_supply_prop *get_psy_prop(struct gb_power_supply *gbpsy, |
| 92 | enum power_supply_property psp) |
| 93 | { |
| 94 | int i; |
| 95 | |
| 96 | for (i = 0; i < gbpsy->properties_count; i++) |
| 97 | if (gbpsy->props[i].prop == psp) |
| 98 | return &gbpsy->props[i]; |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | static int is_psy_prop_writeable(struct gb_power_supply *gbpsy, |
| 103 | enum power_supply_property psp) |
| 104 | { |
| 105 | struct gb_power_supply_prop *prop; |
| 106 | |
| 107 | prop = get_psy_prop(gbpsy, psp); |
| 108 | if (!prop) |
| 109 | return -ENOENT; |
| 110 | return prop->is_writeable ? 1 : 0; |
| 111 | } |
| 112 | |
| 113 | static int is_prop_valint(enum power_supply_property psp) |
| 114 | { |
| 115 | return ((psp < POWER_SUPPLY_PROP_MODEL_NAME) ? 1 : 0); |
| 116 | } |
| 117 | |
| 118 | static void next_interval(struct gb_power_supply *gbpsy) |
| 119 | { |
| 120 | if (gbpsy->update_interval == update_interval_max) |
| 121 | return; |
| 122 | |
| 123 | /* do some exponential back-off in the update interval */ |
| 124 | gbpsy->update_interval *= 2; |
| 125 | if (gbpsy->update_interval > update_interval_max) |
| 126 | gbpsy->update_interval = update_interval_max; |
| 127 | } |
| 128 | |
| 129 | #ifdef DRIVER_OWNS_PSY_STRUCT |
| 130 | static void __gb_power_supply_changed(struct gb_power_supply *gbpsy) |
| 131 | { |
| 132 | power_supply_changed(&gbpsy->psy); |
| 133 | } |
| 134 | #else |
| 135 | static void __gb_power_supply_changed(struct gb_power_supply *gbpsy) |
| 136 | { |
| 137 | power_supply_changed(gbpsy->psy); |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | static void check_changed(struct gb_power_supply *gbpsy, |
| 142 | struct gb_power_supply_prop *prop) |
| 143 | { |
| 144 | const struct gb_power_supply_changes *psyc; |
| 145 | u32 val = prop->val; |
| 146 | u32 prev_val = prop->previous_val; |
| 147 | int i; |
| 148 | |
| 149 | for (i = 0; i < ARRAY_SIZE(psy_props_changes); i++) { |
| 150 | psyc = &psy_props_changes[i]; |
| 151 | if (prop->prop == psyc->prop) { |
| 152 | if (!psyc->tolerance_change) |
| 153 | gbpsy->changed = true; |
| 154 | else if (val < prev_val && |
| 155 | prev_val - val > psyc->tolerance_change) |
| 156 | gbpsy->changed = true; |
| 157 | else if (val > prev_val && |
| 158 | val - prev_val > psyc->tolerance_change) |
| 159 | gbpsy->changed = true; |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static int total_props(struct gb_power_supply *gbpsy) |
| 166 | { |
| 167 | /* this return the intval plus the strval properties */ |
| 168 | return (gbpsy->properties_count + gbpsy->properties_count_str); |
| 169 | } |
| 170 | |
| 171 | static void prop_append(struct gb_power_supply *gbpsy, |
| 172 | enum power_supply_property prop) |
| 173 | { |
| 174 | enum power_supply_property *new_props_raw; |
| 175 | |
| 176 | gbpsy->properties_count_str++; |
| 177 | new_props_raw = krealloc(gbpsy->props_raw, total_props(gbpsy) * |
| 178 | sizeof(enum power_supply_property), |
| 179 | GFP_KERNEL); |
| 180 | if (!new_props_raw) |
| 181 | return; |
| 182 | gbpsy->props_raw = new_props_raw; |
| 183 | gbpsy->props_raw[total_props(gbpsy) - 1] = prop; |
| 184 | } |
| 185 | |
| 186 | static int __gb_power_supply_set_name(char *init_name, char *name, size_t len) |
| 187 | { |
| 188 | unsigned int i = 0; |
| 189 | int ret = 0; |
| 190 | struct power_supply *psy; |
| 191 | |
| 192 | if (!strlen(init_name)) |
| 193 | init_name = "gb_power_supply"; |
| 194 | strlcpy(name, init_name, len); |
| 195 | |
| 196 | while ((ret < len) && (psy = power_supply_get_by_name(name))) { |
| 197 | #ifdef PSY_HAVE_PUT |
| 198 | power_supply_put(psy); |
| 199 | #endif |
| 200 | ret = snprintf(name, len, "%s_%u", init_name, ++i); |
| 201 | } |
| 202 | if (ret >= len) |
| 203 | return -ENOMEM; |
| 204 | return i; |
| 205 | } |
| 206 | |
| 207 | static void _gb_power_supply_append_props(struct gb_power_supply *gbpsy) |
| 208 | { |
| 209 | if (strlen(gbpsy->manufacturer)) |
| 210 | prop_append(gbpsy, POWER_SUPPLY_PROP_MANUFACTURER); |
| 211 | if (strlen(gbpsy->model_name)) |
| 212 | prop_append(gbpsy, POWER_SUPPLY_PROP_MODEL_NAME); |
| 213 | if (strlen(gbpsy->serial_number)) |
| 214 | prop_append(gbpsy, POWER_SUPPLY_PROP_SERIAL_NUMBER); |
| 215 | } |
| 216 | |
| 217 | static int gb_power_supply_description_get(struct gb_power_supply *gbpsy) |
| 218 | { |
| 219 | struct gb_connection *connection = get_conn_from_psy(gbpsy); |
| 220 | struct gb_power_supply_get_description_request req; |
| 221 | struct gb_power_supply_get_description_response resp; |
| 222 | int ret; |
| 223 | |
| 224 | req.psy_id = gbpsy->id; |
| 225 | |
| 226 | ret = gb_operation_sync(connection, |
| 227 | GB_POWER_SUPPLY_TYPE_GET_DESCRIPTION, |
| 228 | &req, sizeof(req), &resp, sizeof(resp)); |
| 229 | if (ret < 0) |
| 230 | return ret; |
| 231 | |
| 232 | gbpsy->manufacturer = kstrndup(resp.manufacturer, PROP_MAX, GFP_KERNEL); |
| 233 | if (!gbpsy->manufacturer) |
| 234 | return -ENOMEM; |
| 235 | gbpsy->model_name = kstrndup(resp.model, PROP_MAX, GFP_KERNEL); |
| 236 | if (!gbpsy->model_name) |
| 237 | return -ENOMEM; |
| 238 | gbpsy->serial_number = kstrndup(resp.serial_number, PROP_MAX, |
| 239 | GFP_KERNEL); |
| 240 | if (!gbpsy->serial_number) |
| 241 | return -ENOMEM; |
| 242 | |
| 243 | gbpsy->type = le16_to_cpu(resp.type); |
| 244 | gbpsy->properties_count = resp.properties_count; |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int gb_power_supply_prop_descriptors_get(struct gb_power_supply *gbpsy) |
| 250 | { |
| 251 | struct gb_connection *connection = get_conn_from_psy(gbpsy); |
| 252 | struct gb_power_supply_get_property_descriptors_request req; |
| 253 | struct gb_power_supply_get_property_descriptors_response resp; |
| 254 | int ret; |
| 255 | int i; |
| 256 | |
| 257 | if (gbpsy->properties_count == 0) |
| 258 | return 0; |
| 259 | |
| 260 | req.psy_id = gbpsy->id; |
| 261 | |
| 262 | ret = gb_operation_sync(connection, |
| 263 | GB_POWER_SUPPLY_TYPE_GET_PROP_DESCRIPTORS, |
| 264 | &req, sizeof(req), &resp, |
| 265 | sizeof(resp) + gbpsy->properties_count * |
| 266 | sizeof(struct gb_power_supply_props_desc)); |
| 267 | if (ret < 0) |
| 268 | return ret; |
| 269 | |
| 270 | gbpsy->props = kcalloc(gbpsy->properties_count, sizeof(*gbpsy->props), |
| 271 | GFP_KERNEL); |
| 272 | if (!gbpsy->props) |
| 273 | return -ENOMEM; |
| 274 | |
| 275 | gbpsy->props_raw = kzalloc(gbpsy->properties_count * |
| 276 | sizeof(*gbpsy->props_raw), GFP_KERNEL); |
| 277 | if (!gbpsy->props_raw) |
| 278 | return -ENOMEM; |
| 279 | |
| 280 | /* Store available properties */ |
| 281 | for (i = 0; i < gbpsy->properties_count; i++) { |
| 282 | gbpsy->props[i].prop = resp.props[i].property; |
| 283 | gbpsy->props_raw[i] = resp.props[i].property; |
| 284 | if (resp.props[i].is_writeable) |
| 285 | gbpsy->props[i].is_writeable = true; |
| 286 | } |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 287 | |
| 288 | /* |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 289 | * now append the properties that we already got information in the |
| 290 | * get_description operation. (char * ones) |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 291 | */ |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 292 | _gb_power_supply_append_props(gbpsy); |
| 293 | |
| 294 | return 0; |
Greg Kroah-Hartman | 43789c3 | 2014-10-20 15:09:49 +0800 | [diff] [blame] | 295 | } |
| 296 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 297 | static int __gb_power_supply_property_update(struct gb_power_supply *gbpsy, |
| 298 | enum power_supply_property psp) |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 299 | { |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 300 | struct gb_connection *connection = get_conn_from_psy(gbpsy); |
| 301 | struct gb_power_supply_prop *prop; |
| 302 | struct gb_power_supply_get_property_request req; |
| 303 | struct gb_power_supply_get_property_response resp; |
| 304 | u32 val; |
| 305 | int ret; |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 306 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 307 | prop = get_psy_prop(gbpsy, psp); |
| 308 | if (!prop) |
| 309 | return -EINVAL; |
| 310 | req.psy_id = gbpsy->id; |
| 311 | req.property = (u8)psp; |
| 312 | |
| 313 | ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_GET_PROPERTY, |
| 314 | &req, sizeof(req), &resp, sizeof(resp)); |
| 315 | if (ret < 0) |
| 316 | return ret; |
| 317 | |
| 318 | val = le32_to_cpu(resp.prop_val); |
| 319 | if (val == prop->val) |
| 320 | return 0; |
| 321 | |
| 322 | prop->previous_val = prop->val; |
| 323 | prop->val = val; |
| 324 | |
| 325 | check_changed(gbpsy, prop); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int __gb_power_supply_property_get(struct gb_power_supply *gbpsy, |
| 331 | enum power_supply_property psp, |
| 332 | union power_supply_propval *val) |
| 333 | { |
| 334 | struct gb_power_supply_prop *prop; |
| 335 | |
| 336 | prop = get_psy_prop(gbpsy, psp); |
| 337 | if (!prop) |
| 338 | return -EINVAL; |
| 339 | |
| 340 | val->intval = prop->val; |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int __gb_power_supply_property_strval_get(struct gb_power_supply *gbpsy, |
| 345 | enum power_supply_property psp, |
| 346 | union power_supply_propval *val) |
| 347 | { |
| 348 | switch (psp) { |
| 349 | case POWER_SUPPLY_PROP_MODEL_NAME: |
Rui Miguel Silva | f921fb1 | 2016-01-08 13:53:46 +0000 | [diff] [blame^] | 350 | val->strval = gbpsy->model_name; |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 351 | break; |
| 352 | case POWER_SUPPLY_PROP_MANUFACTURER: |
Rui Miguel Silva | f921fb1 | 2016-01-08 13:53:46 +0000 | [diff] [blame^] | 353 | val->strval = gbpsy->manufacturer; |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 354 | break; |
| 355 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
Rui Miguel Silva | f921fb1 | 2016-01-08 13:53:46 +0000 | [diff] [blame^] | 356 | val->strval = gbpsy->serial_number; |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 357 | break; |
| 358 | default: |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int _gb_power_supply_property_get(struct gb_power_supply *gbpsy, |
| 366 | enum power_supply_property psp, |
| 367 | union power_supply_propval *val) |
| 368 | { |
| 369 | struct gb_connection *connection = get_conn_from_psy(gbpsy); |
| 370 | int ret; |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 371 | |
| 372 | /* |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 373 | * Properties of type const char *, were already fetched on |
| 374 | * get_description operation and should be cached in gb |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 375 | */ |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 376 | if (is_prop_valint(psp)) |
| 377 | ret = __gb_power_supply_property_get(gbpsy, psp, val); |
| 378 | else |
| 379 | ret = __gb_power_supply_property_strval_get(gbpsy, psp, val); |
| 380 | |
| 381 | if (ret < 0) |
| 382 | dev_err(&connection->bundle->dev, "get property %u\n", psp); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int gb_power_supply_status_get(struct gb_power_supply *gbpsy) |
| 388 | { |
| 389 | int ret = 0; |
| 390 | int i; |
| 391 | |
| 392 | /* check if cache is good enough */ |
| 393 | if (gbpsy->last_update && |
| 394 | time_is_after_jiffies(gbpsy->last_update + |
| 395 | msecs_to_jiffies(cache_time))) |
| 396 | return 0; |
| 397 | |
| 398 | for (i = 0; i < gbpsy->properties_count; i++) { |
| 399 | ret = __gb_power_supply_property_update(gbpsy, |
| 400 | gbpsy->props[i].prop); |
| 401 | if (ret < 0) |
| 402 | break; |
Greg Kroah-Hartman | 0369a45 | 2014-10-21 16:25:13 +0800 | [diff] [blame] | 403 | } |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 404 | |
| 405 | if (ret == 0) |
| 406 | gbpsy->last_update = jiffies; |
| 407 | |
| 408 | return ret; |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 411 | static void gb_power_supply_status_update(struct gb_power_supply *gbpsy) |
Greg Kroah-Hartman | 43789c3 | 2014-10-20 15:09:49 +0800 | [diff] [blame] | 412 | { |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 413 | /* check if there a change that need to be reported */ |
| 414 | gb_power_supply_status_get(gbpsy); |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 415 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 416 | if (!gbpsy->changed) |
| 417 | return; |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 418 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 419 | gbpsy->update_interval = update_interval_init; |
| 420 | __gb_power_supply_changed(gbpsy); |
| 421 | gbpsy->changed = false; |
Greg Kroah-Hartman | 43789c3 | 2014-10-20 15:09:49 +0800 | [diff] [blame] | 422 | } |
| 423 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 424 | static void gb_power_supply_work(struct work_struct *work) |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 425 | { |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 426 | struct gb_power_supply *gbpsy = container_of(work, |
| 427 | struct gb_power_supply, |
| 428 | work.work); |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 429 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 430 | /* |
| 431 | * if the poll interval is not set, disable polling, this is helpful |
| 432 | * specially at unregister time. |
| 433 | */ |
| 434 | if (!gbpsy->update_interval) |
| 435 | return; |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 436 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 437 | gb_power_supply_status_update(gbpsy); |
| 438 | next_interval(gbpsy); |
| 439 | schedule_delayed_work(&gbpsy->work, gbpsy->update_interval); |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 442 | static int get_property(struct power_supply *b, |
| 443 | enum power_supply_property psp, |
| 444 | union power_supply_propval *val) |
| 445 | { |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 446 | struct gb_power_supply *gbpsy = to_gb_power_supply(b); |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 447 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 448 | gb_power_supply_status_get(gbpsy); |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 449 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 450 | return _gb_power_supply_property_get(gbpsy, psp, val); |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 453 | static int gb_power_supply_property_set(struct gb_power_supply *gbpsy, |
| 454 | enum power_supply_property psp, |
| 455 | int val) |
| 456 | { |
| 457 | struct gb_connection *connection = get_conn_from_psy(gbpsy); |
| 458 | struct gb_power_supply_prop *prop; |
| 459 | struct gb_power_supply_set_property_request req; |
| 460 | int ret; |
| 461 | |
| 462 | prop = get_psy_prop(gbpsy, psp); |
| 463 | if (!prop) |
| 464 | return -EINVAL; |
| 465 | req.psy_id = gbpsy->id; |
| 466 | req.property = (u8)psp; |
| 467 | req.prop_val = cpu_to_le32(val); |
| 468 | |
| 469 | ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_SET_PROPERTY, |
| 470 | &req, sizeof(req), NULL, 0); |
| 471 | if (ret < 0) |
| 472 | goto out; |
| 473 | |
| 474 | /* cache immediately the new value */ |
| 475 | prop->val = val; |
| 476 | |
| 477 | out: |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | static int set_property(struct power_supply *b, |
| 482 | enum power_supply_property psp, |
| 483 | const union power_supply_propval *val) |
| 484 | { |
| 485 | struct gb_power_supply *gbpsy = to_gb_power_supply(b); |
| 486 | |
| 487 | return gb_power_supply_property_set(gbpsy, psp, val->intval); |
| 488 | } |
| 489 | |
| 490 | static int property_is_writeable(struct power_supply *b, |
| 491 | enum power_supply_property psp) |
| 492 | { |
| 493 | struct gb_power_supply *gbpsy = to_gb_power_supply(b); |
| 494 | |
| 495 | return is_psy_prop_writeable(gbpsy, psp); |
| 496 | } |
| 497 | |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 498 | |
Alex Elder | 5c58640 | 2015-05-07 13:00:21 -0500 | [diff] [blame] | 499 | #ifdef DRIVER_OWNS_PSY_STRUCT |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 500 | static int gb_power_supply_register(struct gb_power_supply *gbpsy) |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 501 | { |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 502 | struct gb_connection *connection = get_conn_from_psy(gbpsy); |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 503 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 504 | gbpsy->psy.name = gbpsy->name; |
| 505 | gbpsy->psy.type = gbpsy->type; |
| 506 | gbpsy->psy.properties = gbpsy->props_raw; |
| 507 | gbpsy->psy.num_properties = total_props(gbpsy); |
| 508 | gbpsy->psy.get_property = get_property; |
| 509 | gbpsy->psy.set_property = set_property; |
| 510 | gbpsy->psy.property_is_writeable = property_is_writeable; |
| 511 | |
| 512 | return power_supply_register(&connection->bundle->dev, |
| 513 | &gbpsy->psy); |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 514 | } |
| 515 | #else |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 516 | static int gb_power_supply_register(struct gb_power_supply *gbpsy) |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 517 | { |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 518 | struct gb_connection *connection = get_conn_from_psy(gbpsy); |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 519 | struct power_supply_config cfg = {}; |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 520 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 521 | cfg.drv_data = gbpsy; |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 522 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 523 | gbpsy->desc.name = gbpsy->name; |
| 524 | gbpsy->desc.type = gbpsy->type; |
| 525 | gbpsy->desc.properties = gbpsy->props_raw; |
| 526 | gbpsy->desc.num_properties = total_props(gbpsy); |
| 527 | gbpsy->desc.get_property = get_property; |
| 528 | gbpsy->desc.set_property = set_property; |
| 529 | gbpsy->desc.property_is_writeable = property_is_writeable; |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 530 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 531 | gbpsy->psy = power_supply_register(&connection->bundle->dev, |
| 532 | &gbpsy->desc, &cfg); |
| 533 | if (IS_ERR(gbpsy->psy)) |
| 534 | return PTR_ERR(gbpsy->psy); |
Alex Elder | 9ade6d3 | 2015-05-07 13:00:20 -0500 | [diff] [blame] | 535 | |
| 536 | return 0; |
Greg Kroah-Hartman | a549be51 | 2015-05-01 20:41:00 +0200 | [diff] [blame] | 537 | } |
| 538 | #endif |
| 539 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 540 | static void _gb_power_supply_free(struct gb_power_supply *gbpsy) |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 541 | { |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 542 | kfree(gbpsy->serial_number); |
| 543 | kfree(gbpsy->model_name); |
| 544 | kfree(gbpsy->manufacturer); |
| 545 | kfree(gbpsy->props_raw); |
| 546 | kfree(gbpsy->props); |
| 547 | kfree(gbpsy); |
| 548 | } |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 549 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 550 | static void _gb_power_supply_release(struct gb_power_supply *gbpsy) |
| 551 | { |
| 552 | if (!gbpsy) |
| 553 | return; |
| 554 | |
| 555 | gbpsy->update_interval = 0; |
| 556 | |
| 557 | cancel_delayed_work_sync(&gbpsy->work); |
| 558 | #ifdef DRIVER_OWNS_PSY_STRUCT |
Rui Miguel Silva | ff85f72 | 2016-01-08 13:53:43 +0000 | [diff] [blame] | 559 | if (gbpsy->registered) |
| 560 | power_supply_unregister(&gbpsy->psy); |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 561 | #else |
Rui Miguel Silva | ff85f72 | 2016-01-08 13:53:43 +0000 | [diff] [blame] | 562 | if (gbpsy->registered) |
| 563 | power_supply_unregister(gbpsy->psy); |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 564 | #endif |
| 565 | |
| 566 | _gb_power_supply_free(gbpsy); |
| 567 | } |
| 568 | |
| 569 | static void _gb_power_supplies_release(struct gb_power_supplies *supplies) |
| 570 | { |
| 571 | int i; |
| 572 | |
Rui Miguel Silva | 7ccac20d | 2016-01-08 13:53:42 +0000 | [diff] [blame] | 573 | if (!supplies->supply) |
| 574 | return; |
| 575 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 576 | mutex_lock(&supplies->supplies_lock); |
| 577 | for (i = 0; i < supplies->supplies_count; i++) |
| 578 | _gb_power_supply_release(&supplies->supply[i]); |
| 579 | mutex_unlock(&supplies->supplies_lock); |
Rui Miguel Silva | 23f25ba | 2016-01-08 13:53:44 +0000 | [diff] [blame] | 580 | kfree(supplies); |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static int gb_power_supplies_get_count(struct gb_power_supplies *supplies) |
| 584 | { |
| 585 | struct gb_power_supply_get_supplies_response resp; |
| 586 | int ret; |
| 587 | |
| 588 | ret = gb_operation_sync(supplies->connection, |
| 589 | GB_POWER_SUPPLY_TYPE_GET_SUPPLIES, |
| 590 | NULL, 0, &resp, sizeof(resp)); |
| 591 | if (ret < 0) |
| 592 | return ret; |
| 593 | |
| 594 | if (!resp.supplies_count) |
| 595 | return -EINVAL; |
| 596 | |
| 597 | supplies->supplies_count = resp.supplies_count; |
| 598 | |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | static int gb_power_supply_config(struct gb_power_supplies *supplies, int id) |
| 603 | { |
| 604 | struct gb_power_supply *gbpsy = &supplies->supply[id]; |
| 605 | int ret; |
| 606 | |
| 607 | gbpsy->supplies = supplies; |
| 608 | gbpsy->id = id; |
| 609 | |
| 610 | ret = gb_power_supply_description_get(gbpsy); |
| 611 | if (ret < 0) |
| 612 | goto out; |
| 613 | |
| 614 | ret = gb_power_supply_prop_descriptors_get(gbpsy); |
| 615 | if (ret < 0) |
| 616 | goto out; |
| 617 | |
| 618 | /* guarantee that we have an unique name, before register */ |
| 619 | ret = __gb_power_supply_set_name(gbpsy->model_name, gbpsy->name, |
| 620 | sizeof(gbpsy->name)); |
| 621 | if (ret < 0) |
| 622 | goto out; |
| 623 | |
| 624 | ret = gb_power_supply_register(gbpsy); |
| 625 | if (ret < 0) |
| 626 | goto out; |
| 627 | |
| 628 | gbpsy->update_interval = update_interval_init; |
| 629 | INIT_DELAYED_WORK(&gbpsy->work, gb_power_supply_work); |
| 630 | schedule_delayed_work(&gbpsy->work, 0); |
| 631 | |
| 632 | out: |
Rui Miguel Silva | ff85f72 | 2016-01-08 13:53:43 +0000 | [diff] [blame] | 633 | /* if everything went fine just mark it for release code to know */ |
| 634 | if (ret == 0) |
| 635 | gbpsy->registered = true; |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static int gb_power_supplies_setup(struct gb_power_supplies *supplies) |
| 640 | { |
| 641 | struct gb_connection *connection = supplies->connection; |
| 642 | int ret; |
| 643 | int i; |
| 644 | |
| 645 | mutex_lock(&supplies->supplies_lock); |
| 646 | |
| 647 | ret = gb_power_supplies_get_count(supplies); |
| 648 | if (ret < 0) |
| 649 | goto out; |
| 650 | |
| 651 | supplies->supply = kzalloc(supplies->supplies_count * |
| 652 | sizeof(struct gb_power_supply), |
| 653 | GFP_KERNEL); |
| 654 | |
Johan Hovold | e0d91ff | 2016-01-07 12:28:29 +0100 | [diff] [blame] | 655 | if (!supplies->supply) { |
| 656 | ret = -ENOMEM; |
| 657 | goto out; |
| 658 | } |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 659 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 660 | for (i = 0; i < supplies->supplies_count; i++) { |
| 661 | ret = gb_power_supply_config(supplies, i); |
| 662 | if (ret < 0) { |
| 663 | dev_err(&connection->bundle->dev, |
| 664 | "Fail to configure supplies devices\n"); |
| 665 | goto out; |
| 666 | } |
| 667 | } |
| 668 | out: |
| 669 | mutex_unlock(&supplies->supplies_lock); |
| 670 | return ret; |
| 671 | } |
Greg Kroah-Hartman | 2bb7eae | 2014-10-20 15:24:57 +0800 | [diff] [blame] | 672 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 673 | static int gb_power_supply_event_recv(u8 type, struct gb_operation *op) |
| 674 | { |
| 675 | struct gb_connection *connection = op->connection; |
| 676 | struct gb_power_supplies *supplies = connection->private; |
| 677 | struct gb_power_supply *gbpsy; |
| 678 | struct gb_message *request; |
| 679 | struct gb_power_supply_event_request *payload; |
| 680 | u8 psy_id; |
| 681 | u8 event; |
| 682 | int ret = 0; |
Greg Kroah-Hartman | c0855bf | 2014-10-21 14:31:24 +0800 | [diff] [blame] | 683 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 684 | if (type != GB_POWER_SUPPLY_TYPE_EVENT) { |
| 685 | dev_err(&connection->bundle->dev, |
| 686 | "Unsupported unsolicited event: %u\n", type); |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | |
| 690 | request = op->request; |
| 691 | |
| 692 | if (request->payload_size < sizeof(*payload)) { |
| 693 | dev_err(&connection->bundle->dev, |
| 694 | "Wrong event size received (%zu < %zu)\n", |
| 695 | request->payload_size, sizeof(*payload)); |
| 696 | return -EINVAL; |
| 697 | } |
| 698 | |
| 699 | payload = request->payload; |
| 700 | psy_id = payload->psy_id; |
| 701 | mutex_lock(&supplies->supplies_lock); |
| 702 | if (psy_id >= supplies->supplies_count || !&supplies->supply[psy_id]) { |
| 703 | dev_err(&connection->bundle->dev, |
| 704 | "Event received for unconfigured power_supply id: %d\n", |
| 705 | psy_id); |
| 706 | ret = -EINVAL; |
| 707 | goto out_unlock; |
| 708 | } |
| 709 | |
| 710 | event = payload->event; |
| 711 | /* |
| 712 | * we will only handle events after setup is done and before release is |
| 713 | * running. For that just check update_interval. |
| 714 | */ |
| 715 | gbpsy = &supplies->supply[psy_id]; |
| 716 | if (gbpsy->update_interval) { |
| 717 | ret = -ESHUTDOWN; |
| 718 | goto out_unlock; |
| 719 | } |
| 720 | |
| 721 | if (event & GB_POWER_SUPPLY_UPDATE) |
| 722 | gb_power_supply_status_update(gbpsy); |
| 723 | |
| 724 | out_unlock: |
| 725 | mutex_unlock(&supplies->supplies_lock); |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | static int gb_power_supply_connection_init(struct gb_connection *connection) |
| 730 | { |
| 731 | struct gb_power_supplies *supplies; |
Rui Miguel Silva | d9eafd5 | 2016-01-08 13:53:45 +0000 | [diff] [blame] | 732 | int ret; |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 733 | |
| 734 | supplies = kzalloc(sizeof(*supplies), GFP_KERNEL); |
| 735 | if (!supplies) |
| 736 | return -ENOMEM; |
| 737 | |
| 738 | supplies->connection = connection; |
| 739 | connection->private = supplies; |
| 740 | |
| 741 | mutex_init(&supplies->supplies_lock); |
| 742 | |
Rui Miguel Silva | d9eafd5 | 2016-01-08 13:53:45 +0000 | [diff] [blame] | 743 | ret = gb_power_supplies_setup(supplies); |
| 744 | if (ret < 0) |
| 745 | _gb_power_supplies_release(supplies); |
| 746 | |
| 747 | return ret; |
Greg Kroah-Hartman | 33ea3a3 | 2014-09-07 15:39:34 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Rui Miguel Silva | 2724be0 | 2015-11-12 15:36:00 +0000 | [diff] [blame] | 750 | static void gb_power_supply_connection_exit(struct gb_connection *connection) |
Alex Elder | 697e55d | 2014-10-20 23:01:04 -0500 | [diff] [blame] | 751 | { |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 752 | struct gb_power_supplies *supplies = connection->private; |
Alex Elder | 697e55d | 2014-10-20 23:01:04 -0500 | [diff] [blame] | 753 | |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 754 | _gb_power_supplies_release(supplies); |
Alex Elder | 697e55d | 2014-10-20 23:01:04 -0500 | [diff] [blame] | 755 | } |
| 756 | |
Rui Miguel Silva | 2724be0 | 2015-11-12 15:36:00 +0000 | [diff] [blame] | 757 | static struct gb_protocol power_supply_protocol = { |
| 758 | .name = "power_supply", |
| 759 | .id = GREYBUS_PROTOCOL_POWER_SUPPLY, |
| 760 | .major = GB_POWER_SUPPLY_VERSION_MAJOR, |
| 761 | .minor = GB_POWER_SUPPLY_VERSION_MINOR, |
| 762 | .connection_init = gb_power_supply_connection_init, |
| 763 | .connection_exit = gb_power_supply_connection_exit, |
Rui Miguel Silva | ffe2e24 | 2015-11-12 15:36:02 +0000 | [diff] [blame] | 764 | .request_recv = gb_power_supply_event_recv, |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 765 | }; |
| 766 | |
Rui Miguel Silva | 2724be0 | 2015-11-12 15:36:00 +0000 | [diff] [blame] | 767 | gb_protocol_driver(&power_supply_protocol); |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 768 | |
Greg Kroah-Hartman | 7dd2626 | 2014-12-24 13:01:42 -0800 | [diff] [blame] | 769 | MODULE_LICENSE("GPL v2"); |