platform-drivers: x86: pmic: Use irq_chip buslock mechanism

The set_type function of the pmic irq chip is a horrible hack. It
schedules work because it cannot access the scu chip from the set_type
function. That breaks the assumption, that the type is set after
set_type has returned.

irq_chips provide buslock functions to avoid the above. Convert the
driver to use the proper model.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Feng Tang <feng.tang@intel.com>
Cc: Matthew Garrett <mjg@redhat.com>
Cc: Alan Cox <alan@linux.intel.com>
Cc: Alek Du <alek.du@intel.com>
Signed-off-by: Matthew Garrett <mjg@redhat.com>

authored by Thomas Gleixner and committed by Matthew Garrett d4b7de61 cb8e5e6a

+32 -48
+32 -48
drivers/platform/x86/intel_pmic_gpio.c
··· 60 60 #define GPOSW_DOU 0x08 61 61 #define GPOSW_RDRV 0x30 62 62 63 + #define GPIO_UPDATE_TYPE 0x80000000 63 64 64 65 #define NUM_GPIO 24 65 66 66 - struct pmic_gpio_irq { 67 - spinlock_t lock; 68 - u32 trigger[NUM_GPIO]; 69 - u32 dirty; 70 - struct work_struct work; 71 - }; 72 - 73 - 74 67 struct pmic_gpio { 68 + struct mutex buslock; 75 69 struct gpio_chip chip; 76 - struct pmic_gpio_irq irqtypes; 77 70 void *gpiointr; 78 71 int irq; 79 72 unsigned irq_base; 73 + unsigned int update_type; 74 + u32 trigger_type; 80 75 }; 81 76 82 77 static void pmic_program_irqtype(int gpio, int type) ··· 86 91 else 87 92 intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x10); 88 93 }; 89 - 90 - static void pmic_irqtype_work(struct work_struct *work) 91 - { 92 - struct pmic_gpio_irq *t = 93 - container_of(work, struct pmic_gpio_irq, work); 94 - unsigned long flags; 95 - int i; 96 - u16 type; 97 - 98 - spin_lock_irqsave(&t->lock, flags); 99 - /* As we drop the lock, we may need multiple scans if we race the 100 - pmic_irq_type function */ 101 - while (t->dirty) { 102 - /* 103 - * For each pin that has the dirty bit set send an IPC 104 - * message to configure the hardware via the PMIC 105 - */ 106 - for (i = 0; i < NUM_GPIO; i++) { 107 - if (!(t->dirty & (1 << i))) 108 - continue; 109 - t->dirty &= ~(1 << i); 110 - /* We can't trust the array entry or dirty 111 - once the lock is dropped */ 112 - type = t->trigger[i]; 113 - spin_unlock_irqrestore(&t->lock, flags); 114 - pmic_program_irqtype(i, type); 115 - spin_lock_irqsave(&t->lock, flags); 116 - } 117 - } 118 - spin_unlock_irqrestore(&t->lock, flags); 119 - } 120 94 121 95 static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 122 96 { ··· 154 190 1 << (offset - 16)); 155 191 } 156 192 193 + /* 194 + * This is called from genirq with pg->buslock locked and 195 + * irq_desc->lock held. We can not access the scu bus here, so we 196 + * store the change and update in the bus_sync_unlock() function below 197 + */ 157 198 static int pmic_irq_type(struct irq_data *data, unsigned type) 158 199 { 159 200 struct pmic_gpio *pg = irq_data_get_irq_chip_data(data); 160 201 u32 gpio = data->irq - pg->irq_base; 161 - unsigned long flags; 162 202 163 203 if (gpio >= pg->chip.ngpio) 164 204 return -EINVAL; 165 205 166 - spin_lock_irqsave(&pg->irqtypes.lock, flags); 167 - pg->irqtypes.trigger[gpio] = type; 168 - pg->irqtypes.dirty |= (1 << gpio); 169 - spin_unlock_irqrestore(&pg->irqtypes.lock, flags); 170 - schedule_work(&pg->irqtypes.work); 206 + pg->trigger_type = type; 207 + pg->update_type = gpio | GPIO_UPDATE_TYPE; 171 208 return 0; 172 209 } 173 210 ··· 177 212 struct pmic_gpio *pg = container_of(chip, struct pmic_gpio, chip); 178 213 179 214 return pg->irq_base + offset; 215 + } 216 + 217 + static void pmic_bus_lock(struct irq_data *data) 218 + { 219 + struct pmic_gpio *pg = irq_data_get_irq_chip_data(data); 220 + 221 + mutex_lock(&pg->buslock); 222 + } 223 + 224 + static void pmic_bus_sync_unlock(struct irq_data *data) 225 + { 226 + struct pmic_gpio *pg = irq_data_get_irq_chip_data(data); 227 + 228 + if (pg->update_type) { 229 + unsigned int gpio = pg->update_type & ~GPIO_UPDATE_TYPE; 230 + 231 + pmic_program_irqtype(gpio, pg->trigger_type); 232 + pg->update_type = 0; 233 + } 234 + mutex_unlock(&pg->buslock); 180 235 } 181 236 182 237 /* the gpiointr register is read-clear, so just do nothing. */ ··· 272 287 pg->chip.can_sleep = 1; 273 288 pg->chip.dev = dev; 274 289 275 - INIT_WORK(&pg->irqtypes.work, pmic_irqtype_work); 276 - spin_lock_init(&pg->irqtypes.lock); 290 + mutex_init(&pg->buslock); 277 291 278 292 pg->chip.dev = dev; 279 293 retval = gpiochip_add(&pg->chip);