1 /* 2 * Copyright 2011 Red Hat Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: Ben Skeggs 23 */ 24 #include "priv.h" 25 26 #include <core/device.h> 27 #include <core/notify.h> 28 29 static int 30 nvkm_gpio_drive(struct nvkm_gpio *gpio, int idx, int line, int dir, int out) 31 { 32 const struct nvkm_gpio_impl *impl = (void *)nv_object(gpio)->oclass; 33 return impl->drive ? impl->drive(gpio, line, dir, out) : -ENODEV; 34 } 35 36 static int 37 nvkm_gpio_sense(struct nvkm_gpio *gpio, int idx, int line) 38 { 39 const struct nvkm_gpio_impl *impl = (void *)nv_object(gpio)->oclass; 40 return impl->sense ? impl->sense(gpio, line) : -ENODEV; 41 } 42 43 static int 44 nvkm_gpio_find(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line, 45 struct dcb_gpio_func *func) 46 { 47 struct nvkm_bios *bios = nvkm_bios(gpio); 48 u8 ver, len; 49 u16 data; 50 51 if (line == 0xff && tag == 0xff) 52 return -EINVAL; 53 54 data = dcb_gpio_match(bios, idx, tag, line, &ver, &len, func); 55 if (data) 56 return 0; 57 58 /* Apple iMac G4 NV18 */ 59 if (nv_device_match(nv_object(gpio), 0x0189, 0x10de, 0x0010)) { 60 if (tag == DCB_GPIO_TVDAC0) { 61 *func = (struct dcb_gpio_func) { 62 .func = DCB_GPIO_TVDAC0, 63 .line = 4, 64 .log[0] = 0, 65 .log[1] = 1, 66 }; 67 return 0; 68 } 69 } 70 71 return -ENOENT; 72 } 73 74 static int 75 nvkm_gpio_set(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line, int state) 76 { 77 struct dcb_gpio_func func; 78 int ret; 79 80 ret = nvkm_gpio_find(gpio, idx, tag, line, &func); 81 if (ret == 0) { 82 int dir = !!(func.log[state] & 0x02); 83 int out = !!(func.log[state] & 0x01); 84 ret = nvkm_gpio_drive(gpio, idx, func.line, dir, out); 85 } 86 87 return ret; 88 } 89 90 static int 91 nvkm_gpio_get(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line) 92 { 93 struct dcb_gpio_func func; 94 int ret; 95 96 ret = nvkm_gpio_find(gpio, idx, tag, line, &func); 97 if (ret == 0) { 98 ret = nvkm_gpio_sense(gpio, idx, func.line); 99 if (ret >= 0) 100 ret = (ret == (func.log[1] & 1)); 101 } 102 103 return ret; 104 } 105 106 static void 107 nvkm_gpio_intr_fini(struct nvkm_event *event, int type, int index) 108 { 109 struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event); 110 const struct nvkm_gpio_impl *impl = (void *)nv_object(gpio)->oclass; 111 impl->intr_mask(gpio, type, 1 << index, 0); 112 } 113 114 static void 115 nvkm_gpio_intr_init(struct nvkm_event *event, int type, int index) 116 { 117 struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event); 118 const struct nvkm_gpio_impl *impl = (void *)nv_object(gpio)->oclass; 119 impl->intr_mask(gpio, type, 1 << index, 1 << index); 120 } 121 122 static int 123 nvkm_gpio_intr_ctor(struct nvkm_object *object, void *data, u32 size, 124 struct nvkm_notify *notify) 125 { 126 struct nvkm_gpio_ntfy_req *req = data; 127 if (!WARN_ON(size != sizeof(*req))) { 128 notify->size = sizeof(struct nvkm_gpio_ntfy_rep); 129 notify->types = req->mask; 130 notify->index = req->line; 131 return 0; 132 } 133 return -EINVAL; 134 } 135 136 static void 137 nvkm_gpio_intr(struct nvkm_subdev *subdev) 138 { 139 struct nvkm_gpio *gpio = nvkm_gpio(subdev); 140 const struct nvkm_gpio_impl *impl = (void *)nv_object(gpio)->oclass; 141 u32 hi, lo, i; 142 143 impl->intr_stat(gpio, &hi, &lo); 144 145 for (i = 0; (hi | lo) && i < impl->lines; i++) { 146 struct nvkm_gpio_ntfy_rep rep = { 147 .mask = (NVKM_GPIO_HI * !!(hi & (1 << i))) | 148 (NVKM_GPIO_LO * !!(lo & (1 << i))), 149 }; 150 nvkm_event_send(&gpio->event, rep.mask, i, &rep, sizeof(rep)); 151 } 152 } 153 154 static const struct nvkm_event_func 155 nvkm_gpio_intr_func = { 156 .ctor = nvkm_gpio_intr_ctor, 157 .init = nvkm_gpio_intr_init, 158 .fini = nvkm_gpio_intr_fini, 159 }; 160 161 int 162 _nvkm_gpio_fini(struct nvkm_object *object, bool suspend) 163 { 164 const struct nvkm_gpio_impl *impl = (void *)object->oclass; 165 struct nvkm_gpio *gpio = nvkm_gpio(object); 166 u32 mask = (1 << impl->lines) - 1; 167 168 impl->intr_mask(gpio, NVKM_GPIO_TOGGLED, mask, 0); 169 impl->intr_stat(gpio, &mask, &mask); 170 171 return nvkm_subdev_fini(&gpio->base, suspend); 172 } 173 174 static struct dmi_system_id gpio_reset_ids[] = { 175 { 176 .ident = "Apple Macbook 10,1", 177 .matches = { 178 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), 179 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro10,1"), 180 } 181 }, 182 { } 183 }; 184 185 int 186 _nvkm_gpio_init(struct nvkm_object *object) 187 { 188 struct nvkm_gpio *gpio = nvkm_gpio(object); 189 int ret; 190 191 ret = nvkm_subdev_init(&gpio->base); 192 if (ret) 193 return ret; 194 195 if (gpio->reset && dmi_check_system(gpio_reset_ids)) 196 gpio->reset(gpio, DCB_GPIO_UNUSED); 197 198 return ret; 199 } 200 201 void 202 _nvkm_gpio_dtor(struct nvkm_object *object) 203 { 204 struct nvkm_gpio *gpio = (void *)object; 205 nvkm_event_fini(&gpio->event); 206 nvkm_subdev_destroy(&gpio->base); 207 } 208 209 int 210 nvkm_gpio_create_(struct nvkm_object *parent, struct nvkm_object *engine, 211 struct nvkm_oclass *oclass, int length, void **pobject) 212 { 213 const struct nvkm_gpio_impl *impl = (void *)oclass; 214 struct nvkm_gpio *gpio; 215 int ret; 216 217 ret = nvkm_subdev_create_(parent, engine, oclass, 0, "GPIO", 218 "gpio", length, pobject); 219 gpio = *pobject; 220 if (ret) 221 return ret; 222 223 gpio->find = nvkm_gpio_find; 224 gpio->set = nvkm_gpio_set; 225 gpio->get = nvkm_gpio_get; 226 gpio->reset = impl->reset; 227 228 ret = nvkm_event_init(&nvkm_gpio_intr_func, 2, impl->lines, 229 &gpio->event); 230 if (ret) 231 return ret; 232 233 nv_subdev(gpio)->intr = nvkm_gpio_intr; 234 return 0; 235 } 236 237 int 238 _nvkm_gpio_ctor(struct nvkm_object *parent, struct nvkm_object *engine, 239 struct nvkm_oclass *oclass, void *data, u32 size, 240 struct nvkm_object **pobject) 241 { 242 struct nvkm_gpio *gpio; 243 int ret; 244 245 ret = nvkm_gpio_create(parent, engine, oclass, &gpio); 246 *pobject = nv_object(gpio); 247 if (ret) 248 return ret; 249 250 return 0; 251 } 252