| 1 | /* $NetBSD: nouveau_subdev_i2c_aux.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2009 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_i2c_aux.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $" ); |
| 29 | |
| 30 | #include <subdev/i2c.h> |
| 31 | |
| 32 | int |
| 33 | nv_rdaux(struct nouveau_i2c_port *port, u32 addr, u8 *data, u8 size) |
| 34 | { |
| 35 | if (port->func->aux) { |
| 36 | if (port->func->acquire) |
| 37 | port->func->acquire(port); |
| 38 | return port->func->aux(port, 9, addr, data, size); |
| 39 | } |
| 40 | return -ENODEV; |
| 41 | } |
| 42 | |
| 43 | int |
| 44 | nv_wraux(struct nouveau_i2c_port *port, u32 addr, u8 *data, u8 size) |
| 45 | { |
| 46 | if (port->func->aux) { |
| 47 | if (port->func->acquire) |
| 48 | port->func->acquire(port); |
| 49 | return port->func->aux(port, 8, addr, data, size); |
| 50 | } |
| 51 | return -ENODEV; |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | aux_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
| 56 | { |
| 57 | struct nouveau_i2c_port *port = adap->algo_data; |
| 58 | struct i2c_msg *msg = msgs; |
| 59 | int ret, mcnt = num; |
| 60 | |
| 61 | if (!port->func->aux) |
| 62 | return -ENODEV; |
| 63 | if ( port->func->acquire) |
| 64 | port->func->acquire(port); |
| 65 | |
| 66 | while (mcnt--) { |
| 67 | u8 remaining = msg->len; |
| 68 | u8 *ptr = msg->buf; |
| 69 | |
| 70 | while (remaining) { |
| 71 | u8 cnt = (remaining > 16) ? 16 : remaining; |
| 72 | u8 cmd; |
| 73 | |
| 74 | if (msg->flags & I2C_M_RD) |
| 75 | cmd = 1; |
| 76 | else |
| 77 | cmd = 0; |
| 78 | |
| 79 | if (mcnt || remaining > 16) |
| 80 | cmd |= 4; /* MOT */ |
| 81 | |
| 82 | ret = port->func->aux(port, cmd, msg->addr, ptr, cnt); |
| 83 | if (ret < 0) |
| 84 | return ret; |
| 85 | |
| 86 | ptr += cnt; |
| 87 | remaining -= cnt; |
| 88 | } |
| 89 | |
| 90 | msg++; |
| 91 | } |
| 92 | |
| 93 | return num; |
| 94 | } |
| 95 | |
| 96 | static u32 |
| 97 | aux_func(struct i2c_adapter *adap) |
| 98 | { |
| 99 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 100 | } |
| 101 | |
| 102 | const struct i2c_algorithm nouveau_i2c_aux_algo = { |
| 103 | .master_xfer = aux_xfer, |
| 104 | .functionality = aux_func |
| 105 | }; |
| 106 | |