| 1 | /* $NetBSD: nouveau_subdev_volt_gpio.c,v 1.1.1.1 2014/08/06 12:36:32 riastradh Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2013 Red Hat Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | * OTHER DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: Ben Skeggs |
| 25 | */ |
| 26 | |
| 27 | #include <sys/cdefs.h> |
| 28 | __KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_volt_gpio.c,v 1.1.1.1 2014/08/06 12:36:32 riastradh Exp $" ); |
| 29 | |
| 30 | #include <subdev/volt.h> |
| 31 | #include <subdev/gpio.h> |
| 32 | #include <subdev/bios/gpio.h> |
| 33 | |
| 34 | static const u8 tags[] = { |
| 35 | DCB_GPIO_VID0, DCB_GPIO_VID1, DCB_GPIO_VID2, DCB_GPIO_VID3, |
| 36 | DCB_GPIO_VID4, DCB_GPIO_VID5, DCB_GPIO_VID6, DCB_GPIO_VID7, |
| 37 | }; |
| 38 | |
| 39 | int |
| 40 | nouveau_voltgpio_get(struct nouveau_volt *volt) |
| 41 | { |
| 42 | struct nouveau_gpio *gpio = nouveau_gpio(volt); |
| 43 | u8 vid = 0; |
| 44 | int i; |
| 45 | |
| 46 | for (i = 0; i < ARRAY_SIZE(tags); i++) { |
| 47 | if (volt->vid_mask & (1 << i)) { |
| 48 | int ret = gpio->get(gpio, 0, tags[i], 0xff); |
| 49 | if (ret < 0) |
| 50 | return ret; |
| 51 | vid |= ret << i; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return vid; |
| 56 | } |
| 57 | |
| 58 | int |
| 59 | nouveau_voltgpio_set(struct nouveau_volt *volt, u8 vid) |
| 60 | { |
| 61 | struct nouveau_gpio *gpio = nouveau_gpio(volt); |
| 62 | int i; |
| 63 | |
| 64 | for (i = 0; i < ARRAY_SIZE(tags); i++, vid >>= 1) { |
| 65 | if (volt->vid_mask & (1 << i)) { |
| 66 | int ret = gpio->set(gpio, 0, tags[i], 0xff, vid & 1); |
| 67 | if (ret < 0) |
| 68 | return ret; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | nouveau_voltgpio_init(struct nouveau_volt *volt) |
| 77 | { |
| 78 | struct nouveau_gpio *gpio = nouveau_gpio(volt); |
| 79 | struct dcb_gpio_func func; |
| 80 | int i; |
| 81 | |
| 82 | /* check we have gpio function info for each vid bit. on some |
| 83 | * boards (ie. nvs295) the vid mask has more bits than there |
| 84 | * are valid gpio functions... from traces, nvidia appear to |
| 85 | * just touch the existing ones, so let's mask off the invalid |
| 86 | * bits and continue with life |
| 87 | */ |
| 88 | for (i = 0; i < ARRAY_SIZE(tags); i++) { |
| 89 | if (volt->vid_mask & (1 << i)) { |
| 90 | int ret = gpio->find(gpio, 0, tags[i], 0xff, &func); |
| 91 | if (ret) { |
| 92 | if (ret != -ENOENT) |
| 93 | return ret; |
| 94 | nv_debug(volt, "VID bit %d has no GPIO\n" , i); |
| 95 | volt->vid_mask &= ~(1 << i); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |