| .. | .. |
|---|
| 27 | 27 | * Pre-requisites: headers required by header of this unit |
|---|
| 28 | 28 | */ |
|---|
| 29 | 29 | |
|---|
| 30 | +#include <linux/slab.h> |
|---|
| 31 | + |
|---|
| 30 | 32 | #include "dm_services.h" |
|---|
| 31 | 33 | #include "include/gpio_interface.h" |
|---|
| 32 | 34 | #include "include/gpio_service_interface.h" |
|---|
| .. | .. |
|---|
| 51 | 53 | */ |
|---|
| 52 | 54 | |
|---|
| 53 | 55 | struct gpio_service *dal_gpio_service_create( |
|---|
| 54 | | - enum dce_version dce_version_major, |
|---|
| 55 | | - enum dce_version dce_version_minor, |
|---|
| 56 | + enum dce_version dce_version, |
|---|
| 57 | + enum dce_environment dce_environment, |
|---|
| 56 | 58 | struct dc_context *ctx) |
|---|
| 57 | 59 | { |
|---|
| 58 | 60 | struct gpio_service *service; |
|---|
| 59 | | - |
|---|
| 60 | 61 | uint32_t index_of_id; |
|---|
| 61 | 62 | |
|---|
| 62 | 63 | service = kzalloc(sizeof(struct gpio_service), GFP_KERNEL); |
|---|
| .. | .. |
|---|
| 66 | 67 | return NULL; |
|---|
| 67 | 68 | } |
|---|
| 68 | 69 | |
|---|
| 69 | | - if (!dal_hw_translate_init(&service->translate, dce_version_major, |
|---|
| 70 | | - dce_version_minor)) { |
|---|
| 70 | + if (!dal_hw_translate_init(&service->translate, dce_version, |
|---|
| 71 | + dce_environment)) { |
|---|
| 71 | 72 | BREAK_TO_DEBUGGER(); |
|---|
| 72 | 73 | goto failure_1; |
|---|
| 73 | 74 | } |
|---|
| 74 | 75 | |
|---|
| 75 | | - if (!dal_hw_factory_init(&service->factory, dce_version_major, |
|---|
| 76 | | - dce_version_minor)) { |
|---|
| 76 | + if (!dal_hw_factory_init(&service->factory, dce_version, |
|---|
| 77 | + dce_environment)) { |
|---|
| 77 | 78 | BREAK_TO_DEBUGGER(); |
|---|
| 78 | 79 | goto failure_1; |
|---|
| 79 | 80 | } |
|---|
| 80 | 81 | |
|---|
| 81 | | - /* allocate and initialize business storage */ |
|---|
| 82 | + /* allocate and initialize busyness storage */ |
|---|
| 82 | 83 | { |
|---|
| 83 | | - const uint32_t bits_per_uint = sizeof(uint32_t) << 3; |
|---|
| 84 | | - |
|---|
| 85 | 84 | index_of_id = 0; |
|---|
| 86 | 85 | service->ctx = ctx; |
|---|
| 87 | 86 | |
|---|
| 88 | 87 | do { |
|---|
| 89 | 88 | uint32_t number_of_bits = |
|---|
| 90 | 89 | service->factory.number_of_pins[index_of_id]; |
|---|
| 90 | + uint32_t i = 0; |
|---|
| 91 | 91 | |
|---|
| 92 | | - uint32_t number_of_uints = |
|---|
| 93 | | - (number_of_bits + bits_per_uint - 1) / |
|---|
| 94 | | - bits_per_uint; |
|---|
| 92 | + if (number_of_bits) { |
|---|
| 93 | + service->busyness[index_of_id] = |
|---|
| 94 | + kcalloc(number_of_bits, sizeof(char), |
|---|
| 95 | + GFP_KERNEL); |
|---|
| 95 | 96 | |
|---|
| 96 | | - uint32_t *slot; |
|---|
| 97 | | - |
|---|
| 98 | | - if (number_of_bits) { |
|---|
| 99 | | - uint32_t index_of_uint = 0; |
|---|
| 100 | | - |
|---|
| 101 | | - slot = kcalloc(number_of_uints, |
|---|
| 102 | | - sizeof(uint32_t), |
|---|
| 103 | | - GFP_KERNEL); |
|---|
| 104 | | - |
|---|
| 105 | | - if (!slot) { |
|---|
| 97 | + if (!service->busyness[index_of_id]) { |
|---|
| 106 | 98 | BREAK_TO_DEBUGGER(); |
|---|
| 107 | 99 | goto failure_2; |
|---|
| 108 | 100 | } |
|---|
| 109 | 101 | |
|---|
| 110 | 102 | do { |
|---|
| 111 | | - slot[index_of_uint] = 0; |
|---|
| 112 | | - |
|---|
| 113 | | - ++index_of_uint; |
|---|
| 114 | | - } while (index_of_uint < number_of_uints); |
|---|
| 115 | | - } else |
|---|
| 116 | | - slot = NULL; |
|---|
| 117 | | - |
|---|
| 118 | | - service->busyness[index_of_id] = slot; |
|---|
| 103 | + service->busyness[index_of_id][i] = 0; |
|---|
| 104 | + ++i; |
|---|
| 105 | + } while (i < number_of_bits); |
|---|
| 106 | + } else { |
|---|
| 107 | + service->busyness[index_of_id] = NULL; |
|---|
| 108 | + } |
|---|
| 119 | 109 | |
|---|
| 120 | 110 | ++index_of_id; |
|---|
| 121 | 111 | } while (index_of_id < GPIO_ID_COUNT); |
|---|
| .. | .. |
|---|
| 125 | 115 | |
|---|
| 126 | 116 | failure_2: |
|---|
| 127 | 117 | while (index_of_id) { |
|---|
| 128 | | - uint32_t *slot; |
|---|
| 129 | | - |
|---|
| 130 | 118 | --index_of_id; |
|---|
| 131 | | - |
|---|
| 132 | | - slot = service->busyness[index_of_id]; |
|---|
| 133 | | - |
|---|
| 134 | | - kfree(slot); |
|---|
| 119 | + kfree(service->busyness[index_of_id]); |
|---|
| 135 | 120 | } |
|---|
| 136 | 121 | |
|---|
| 137 | 122 | failure_1: |
|---|
| .. | .. |
|---|
| 156 | 141 | return dal_gpio_create_irq(service, id, en); |
|---|
| 157 | 142 | } |
|---|
| 158 | 143 | |
|---|
| 144 | +struct gpio *dal_gpio_service_create_generic_mux( |
|---|
| 145 | + struct gpio_service *service, |
|---|
| 146 | + uint32_t offset, |
|---|
| 147 | + uint32_t mask) |
|---|
| 148 | +{ |
|---|
| 149 | + enum gpio_id id; |
|---|
| 150 | + uint32_t en; |
|---|
| 151 | + struct gpio *generic; |
|---|
| 152 | + |
|---|
| 153 | + if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en)) { |
|---|
| 154 | + ASSERT_CRITICAL(false); |
|---|
| 155 | + return NULL; |
|---|
| 156 | + } |
|---|
| 157 | + |
|---|
| 158 | + generic = dal_gpio_create( |
|---|
| 159 | + service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT); |
|---|
| 160 | + |
|---|
| 161 | + return generic; |
|---|
| 162 | +} |
|---|
| 163 | + |
|---|
| 164 | +void dal_gpio_destroy_generic_mux( |
|---|
| 165 | + struct gpio **mux) |
|---|
| 166 | +{ |
|---|
| 167 | + if (!mux || !*mux) { |
|---|
| 168 | + ASSERT_CRITICAL(false); |
|---|
| 169 | + return; |
|---|
| 170 | + } |
|---|
| 171 | + |
|---|
| 172 | + dal_gpio_destroy(mux); |
|---|
| 173 | + kfree(*mux); |
|---|
| 174 | + |
|---|
| 175 | + *mux = NULL; |
|---|
| 176 | +} |
|---|
| 177 | + |
|---|
| 178 | +struct gpio_pin_info dal_gpio_get_generic_pin_info( |
|---|
| 179 | + struct gpio_service *service, |
|---|
| 180 | + enum gpio_id id, |
|---|
| 181 | + uint32_t en) |
|---|
| 182 | +{ |
|---|
| 183 | + struct gpio_pin_info pin; |
|---|
| 184 | + |
|---|
| 185 | + if (service->translate.funcs->id_to_offset) { |
|---|
| 186 | + service->translate.funcs->id_to_offset(id, en, &pin); |
|---|
| 187 | + } else { |
|---|
| 188 | + pin.mask = 0xFFFFFFFF; |
|---|
| 189 | + pin.offset = 0xFFFFFFFF; |
|---|
| 190 | + } |
|---|
| 191 | + |
|---|
| 192 | + return pin; |
|---|
| 193 | +} |
|---|
| 194 | + |
|---|
| 159 | 195 | void dal_gpio_service_destroy( |
|---|
| 160 | 196 | struct gpio_service **ptr) |
|---|
| 161 | 197 | { |
|---|
| .. | .. |
|---|
| 169 | 205 | uint32_t index_of_id = 0; |
|---|
| 170 | 206 | |
|---|
| 171 | 207 | do { |
|---|
| 172 | | - uint32_t *slot = (*ptr)->busyness[index_of_id]; |
|---|
| 173 | | - |
|---|
| 174 | | - kfree(slot); |
|---|
| 208 | + kfree((*ptr)->busyness[index_of_id]); |
|---|
| 175 | 209 | |
|---|
| 176 | 210 | ++index_of_id; |
|---|
| 177 | 211 | } while (index_of_id < GPIO_ID_COUNT); |
|---|
| .. | .. |
|---|
| 180 | 214 | kfree(*ptr); |
|---|
| 181 | 215 | |
|---|
| 182 | 216 | *ptr = NULL; |
|---|
| 217 | +} |
|---|
| 218 | + |
|---|
| 219 | +enum gpio_result dal_mux_setup_config( |
|---|
| 220 | + struct gpio *mux, |
|---|
| 221 | + struct gpio_generic_mux_config *config) |
|---|
| 222 | +{ |
|---|
| 223 | + struct gpio_config_data config_data; |
|---|
| 224 | + |
|---|
| 225 | + if (!config) |
|---|
| 226 | + return GPIO_RESULT_INVALID_DATA; |
|---|
| 227 | + |
|---|
| 228 | + config_data.config.generic_mux = *config; |
|---|
| 229 | + config_data.type = GPIO_CONFIG_TYPE_GENERIC_MUX; |
|---|
| 230 | + |
|---|
| 231 | + return dal_gpio_set_config(mux, &config_data); |
|---|
| 183 | 232 | } |
|---|
| 184 | 233 | |
|---|
| 185 | 234 | /* |
|---|
| .. | .. |
|---|
| 192 | 241 | enum gpio_id id, |
|---|
| 193 | 242 | uint32_t en) |
|---|
| 194 | 243 | { |
|---|
| 195 | | - const uint32_t bits_per_uint = sizeof(uint32_t) << 3; |
|---|
| 196 | | - |
|---|
| 197 | | - const uint32_t *slot = service->busyness[id] + (en / bits_per_uint); |
|---|
| 198 | | - |
|---|
| 199 | | - return 0 != (*slot & (1 << (en % bits_per_uint))); |
|---|
| 244 | + return service->busyness[id][en]; |
|---|
| 200 | 245 | } |
|---|
| 201 | 246 | |
|---|
| 202 | 247 | static void set_pin_busy( |
|---|
| .. | .. |
|---|
| 204 | 249 | enum gpio_id id, |
|---|
| 205 | 250 | uint32_t en) |
|---|
| 206 | 251 | { |
|---|
| 207 | | - const uint32_t bits_per_uint = sizeof(uint32_t) << 3; |
|---|
| 208 | | - |
|---|
| 209 | | - service->busyness[id][en / bits_per_uint] |= |
|---|
| 210 | | - (1 << (en % bits_per_uint)); |
|---|
| 252 | + service->busyness[id][en] = true; |
|---|
| 211 | 253 | } |
|---|
| 212 | 254 | |
|---|
| 213 | 255 | static void set_pin_free( |
|---|
| .. | .. |
|---|
| 215 | 257 | enum gpio_id id, |
|---|
| 216 | 258 | uint32_t en) |
|---|
| 217 | 259 | { |
|---|
| 218 | | - const uint32_t bits_per_uint = sizeof(uint32_t) << 3; |
|---|
| 260 | + service->busyness[id][en] = false; |
|---|
| 261 | +} |
|---|
| 219 | 262 | |
|---|
| 220 | | - service->busyness[id][en / bits_per_uint] &= |
|---|
| 221 | | - ~(1 << (en % bits_per_uint)); |
|---|
| 263 | +enum gpio_result dal_gpio_service_lock( |
|---|
| 264 | + struct gpio_service *service, |
|---|
| 265 | + enum gpio_id id, |
|---|
| 266 | + uint32_t en) |
|---|
| 267 | +{ |
|---|
| 268 | + if (!service->busyness[id]) { |
|---|
| 269 | + ASSERT_CRITICAL(false); |
|---|
| 270 | + return GPIO_RESULT_OPEN_FAILED; |
|---|
| 271 | + } |
|---|
| 272 | + |
|---|
| 273 | + set_pin_busy(service, id, en); |
|---|
| 274 | + return GPIO_RESULT_OK; |
|---|
| 275 | +} |
|---|
| 276 | + |
|---|
| 277 | +enum gpio_result dal_gpio_service_unlock( |
|---|
| 278 | + struct gpio_service *service, |
|---|
| 279 | + enum gpio_id id, |
|---|
| 280 | + uint32_t en) |
|---|
| 281 | +{ |
|---|
| 282 | + if (!service->busyness[id]) { |
|---|
| 283 | + ASSERT_CRITICAL(false); |
|---|
| 284 | + return GPIO_RESULT_OPEN_FAILED; |
|---|
| 285 | + } |
|---|
| 286 | + |
|---|
| 287 | + set_pin_free(service, id, en); |
|---|
| 288 | + return GPIO_RESULT_OK; |
|---|
| 222 | 289 | } |
|---|
| 223 | 290 | |
|---|
| 224 | 291 | enum gpio_result dal_gpio_service_open( |
|---|
| 225 | | - struct gpio_service *service, |
|---|
| 226 | | - enum gpio_id id, |
|---|
| 227 | | - uint32_t en, |
|---|
| 228 | | - enum gpio_mode mode, |
|---|
| 229 | | - struct hw_gpio_pin **ptr) |
|---|
| 292 | + struct gpio *gpio) |
|---|
| 230 | 293 | { |
|---|
| 231 | | - struct hw_gpio_pin *pin; |
|---|
| 294 | + struct gpio_service *service = gpio->service; |
|---|
| 295 | + enum gpio_id id = gpio->id; |
|---|
| 296 | + uint32_t en = gpio->en; |
|---|
| 297 | + enum gpio_mode mode = gpio->mode; |
|---|
| 298 | + |
|---|
| 299 | + struct hw_gpio_pin **pin = &gpio->pin; |
|---|
| 300 | + |
|---|
| 232 | 301 | |
|---|
| 233 | 302 | if (!service->busyness[id]) { |
|---|
| 234 | 303 | ASSERT_CRITICAL(false); |
|---|
| .. | .. |
|---|
| 242 | 311 | |
|---|
| 243 | 312 | switch (id) { |
|---|
| 244 | 313 | case GPIO_ID_DDC_DATA: |
|---|
| 245 | | - pin = service->factory.funcs->create_ddc_data( |
|---|
| 246 | | - service->ctx, id, en); |
|---|
| 247 | | - service->factory.funcs->define_ddc_registers(pin, en); |
|---|
| 314 | + *pin = service->factory.funcs->get_ddc_pin(gpio); |
|---|
| 315 | + service->factory.funcs->define_ddc_registers(*pin, en); |
|---|
| 248 | 316 | break; |
|---|
| 249 | 317 | case GPIO_ID_DDC_CLOCK: |
|---|
| 250 | | - pin = service->factory.funcs->create_ddc_clock( |
|---|
| 251 | | - service->ctx, id, en); |
|---|
| 252 | | - service->factory.funcs->define_ddc_registers(pin, en); |
|---|
| 318 | + *pin = service->factory.funcs->get_ddc_pin(gpio); |
|---|
| 319 | + service->factory.funcs->define_ddc_registers(*pin, en); |
|---|
| 253 | 320 | break; |
|---|
| 254 | 321 | case GPIO_ID_GENERIC: |
|---|
| 255 | | - pin = service->factory.funcs->create_generic( |
|---|
| 256 | | - service->ctx, id, en); |
|---|
| 322 | + *pin = service->factory.funcs->get_generic_pin(gpio); |
|---|
| 323 | + service->factory.funcs->define_generic_registers(*pin, en); |
|---|
| 257 | 324 | break; |
|---|
| 258 | 325 | case GPIO_ID_HPD: |
|---|
| 259 | | - pin = service->factory.funcs->create_hpd( |
|---|
| 260 | | - service->ctx, id, en); |
|---|
| 261 | | - service->factory.funcs->define_hpd_registers(pin, en); |
|---|
| 326 | + *pin = service->factory.funcs->get_hpd_pin(gpio); |
|---|
| 327 | + service->factory.funcs->define_hpd_registers(*pin, en); |
|---|
| 262 | 328 | break; |
|---|
| 329 | + |
|---|
| 330 | + //TODO: gsl and sync support? create_sync and create_gsl are NULL |
|---|
| 263 | 331 | case GPIO_ID_SYNC: |
|---|
| 264 | | - pin = service->factory.funcs->create_sync( |
|---|
| 265 | | - service->ctx, id, en); |
|---|
| 266 | | - break; |
|---|
| 267 | 332 | case GPIO_ID_GSL: |
|---|
| 268 | | - pin = service->factory.funcs->create_gsl( |
|---|
| 269 | | - service->ctx, id, en); |
|---|
| 270 | 333 | break; |
|---|
| 271 | 334 | default: |
|---|
| 272 | 335 | ASSERT_CRITICAL(false); |
|---|
| 273 | 336 | return GPIO_RESULT_NON_SPECIFIC_ERROR; |
|---|
| 274 | 337 | } |
|---|
| 275 | 338 | |
|---|
| 276 | | - if (!pin) { |
|---|
| 339 | + if (!*pin) { |
|---|
| 277 | 340 | ASSERT_CRITICAL(false); |
|---|
| 278 | 341 | return GPIO_RESULT_NON_SPECIFIC_ERROR; |
|---|
| 279 | 342 | } |
|---|
| 280 | 343 | |
|---|
| 281 | | - if (!pin->funcs->open(pin, mode)) { |
|---|
| 344 | + if (!(*pin)->funcs->open(*pin, mode)) { |
|---|
| 282 | 345 | ASSERT_CRITICAL(false); |
|---|
| 283 | | - dal_gpio_service_close(service, &pin); |
|---|
| 346 | + dal_gpio_service_close(service, pin); |
|---|
| 284 | 347 | return GPIO_RESULT_OPEN_FAILED; |
|---|
| 285 | 348 | } |
|---|
| 286 | 349 | |
|---|
| 287 | 350 | set_pin_busy(service, id, en); |
|---|
| 288 | | - *ptr = pin; |
|---|
| 289 | 351 | return GPIO_RESULT_OK; |
|---|
| 290 | 352 | } |
|---|
| 291 | 353 | |
|---|
| .. | .. |
|---|
| 307 | 369 | |
|---|
| 308 | 370 | pin->funcs->close(pin); |
|---|
| 309 | 371 | |
|---|
| 310 | | - pin->funcs->destroy(ptr); |
|---|
| 372 | + *ptr = NULL; |
|---|
| 311 | 373 | } |
|---|
| 312 | 374 | } |
|---|
| 313 | | - |
|---|
| 314 | 375 | |
|---|
| 315 | 376 | enum dc_irq_source dal_irq_get_source( |
|---|
| 316 | 377 | const struct gpio *irq) |
|---|
| .. | .. |
|---|
| 398 | 459 | return; |
|---|
| 399 | 460 | } |
|---|
| 400 | 461 | |
|---|
| 401 | | - dal_gpio_close(*irq); |
|---|
| 402 | 462 | dal_gpio_destroy(irq); |
|---|
| 403 | 463 | kfree(*irq); |
|---|
| 404 | 464 | |
|---|