Home | History | Annotate | Line # | Download | only in radeon
      1  1.1  riastrad /*	$NetBSD: radeon_atombios_i2c.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2011 Advanced Micro Devices, Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Alex Deucher
     25  1.1  riastrad  *
     26  1.1  riastrad  */
     27  1.2  riastrad 
     28  1.1  riastrad #include <sys/cdefs.h>
     29  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_atombios_i2c.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $");
     30  1.1  riastrad 
     31  1.1  riastrad #include <drm/radeon_drm.h>
     32  1.1  riastrad #include "radeon.h"
     33  1.1  riastrad #include "atom.h"
     34  1.1  riastrad 
     35  1.1  riastrad #define TARGET_HW_I2C_CLOCK 50
     36  1.1  riastrad 
     37  1.1  riastrad /* these are a limitation of ProcessI2cChannelTransaction not the hw */
     38  1.1  riastrad #define ATOM_MAX_HW_I2C_WRITE 3
     39  1.1  riastrad #define ATOM_MAX_HW_I2C_READ  255
     40  1.1  riastrad 
     41  1.1  riastrad static int radeon_process_i2c_ch(struct radeon_i2c_chan *chan,
     42  1.1  riastrad 				 u8 slave_addr, u8 flags,
     43  1.2  riastrad 				 u8 *buf, int num)
     44  1.1  riastrad {
     45  1.1  riastrad 	struct drm_device *dev = chan->dev;
     46  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
     47  1.1  riastrad 	PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args;
     48  1.1  riastrad 	int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction);
     49  1.1  riastrad 	unsigned char *base;
     50  1.1  riastrad 	u16 out = cpu_to_le16(0);
     51  1.1  riastrad 	int r = 0;
     52  1.1  riastrad 
     53  1.1  riastrad 	memset(&args, 0, sizeof(args));
     54  1.1  riastrad 
     55  1.1  riastrad 	mutex_lock(&chan->mutex);
     56  1.1  riastrad 	mutex_lock(&rdev->mode_info.atom_context->scratch_mutex);
     57  1.1  riastrad 
     58  1.1  riastrad 	base = (unsigned char *)rdev->mode_info.atom_context->scratch;
     59  1.1  riastrad 
     60  1.1  riastrad 	if (flags & HW_I2C_WRITE) {
     61  1.1  riastrad 		if (num > ATOM_MAX_HW_I2C_WRITE) {
     62  1.1  riastrad 			DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 3)\n", num);
     63  1.1  riastrad 			r = -EINVAL;
     64  1.1  riastrad 			goto done;
     65  1.1  riastrad 		}
     66  1.1  riastrad 		if (buf == NULL)
     67  1.1  riastrad 			args.ucRegIndex = 0;
     68  1.1  riastrad 		else
     69  1.1  riastrad 			args.ucRegIndex = buf[0];
     70  1.1  riastrad 		if (num)
     71  1.1  riastrad 			num--;
     72  1.1  riastrad 		if (num)
     73  1.1  riastrad 			memcpy(&out, &buf[1], num);
     74  1.1  riastrad 		args.lpI2CDataOut = cpu_to_le16(out);
     75  1.1  riastrad 	} else {
     76  1.1  riastrad 		CTASSERT(ATOM_MAX_HW_I2C_READ <
     77  1.1  riastrad 		    (uintmax_t)1 << (CHAR_BIT*sizeof(num)));
     78  1.1  riastrad 		args.ucRegIndex = 0;
     79  1.1  riastrad 		args.lpI2CDataOut = 0;
     80  1.1  riastrad 	}
     81  1.1  riastrad 
     82  1.1  riastrad 	args.ucFlag = flags;
     83  1.1  riastrad 	args.ucI2CSpeed = TARGET_HW_I2C_CLOCK;
     84  1.1  riastrad 	args.ucTransBytes = num;
     85  1.1  riastrad 	args.ucSlaveAddr = slave_addr << 1;
     86  1.1  riastrad 	args.ucLineNumber = chan->rec.i2c_id;
     87  1.1  riastrad 
     88  1.1  riastrad 	atom_execute_table_scratch_unlocked(rdev->mode_info.atom_context, index, (uint32_t *)&args);
     89  1.1  riastrad 
     90  1.1  riastrad 	/* error */
     91  1.1  riastrad 	if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) {
     92  1.1  riastrad 		DRM_DEBUG_KMS("hw_i2c error\n");
     93  1.1  riastrad 		r = -EIO;
     94  1.1  riastrad 		goto done;
     95  1.1  riastrad 	}
     96  1.1  riastrad 
     97  1.1  riastrad 	if (!(flags & HW_I2C_WRITE))
     98  1.1  riastrad 		radeon_atom_copy_swap(buf, base, num, false);
     99  1.1  riastrad 
    100  1.1  riastrad done:
    101  1.1  riastrad 	mutex_unlock(&rdev->mode_info.atom_context->scratch_mutex);
    102  1.1  riastrad 	mutex_unlock(&chan->mutex);
    103  1.1  riastrad 
    104  1.1  riastrad 	return r;
    105  1.1  riastrad }
    106  1.1  riastrad 
    107  1.1  riastrad int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
    108  1.1  riastrad 			    struct i2c_msg *msgs, int num)
    109  1.1  riastrad {
    110  1.1  riastrad 	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
    111  1.1  riastrad 	struct i2c_msg *p;
    112  1.1  riastrad 	int i, remaining, current_count, buffer_offset, max_bytes, ret;
    113  1.1  riastrad 	u8 flags;
    114  1.1  riastrad 
    115  1.1  riastrad 	/* check for bus probe */
    116  1.1  riastrad 	p = &msgs[0];
    117  1.1  riastrad 	if ((num == 1) && (p->len == 0)) {
    118  1.1  riastrad 		ret = radeon_process_i2c_ch(i2c,
    119  1.1  riastrad 					    p->addr, HW_I2C_WRITE,
    120  1.1  riastrad 					    NULL, 0);
    121  1.1  riastrad 		if (ret)
    122  1.1  riastrad 			return ret;
    123  1.1  riastrad 		else
    124  1.1  riastrad 			return num;
    125  1.1  riastrad 	}
    126  1.1  riastrad 
    127  1.1  riastrad 	for (i = 0; i < num; i++) {
    128  1.1  riastrad 		p = &msgs[i];
    129  1.1  riastrad 		remaining = p->len;
    130  1.1  riastrad 		buffer_offset = 0;
    131  1.1  riastrad 		/* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */
    132  1.1  riastrad 		if (p->flags & I2C_M_RD) {
    133  1.1  riastrad 			max_bytes = ATOM_MAX_HW_I2C_READ;
    134  1.1  riastrad 			flags = HW_I2C_READ;
    135  1.1  riastrad 		} else {
    136  1.1  riastrad 			max_bytes = ATOM_MAX_HW_I2C_WRITE;
    137  1.1  riastrad 			flags = HW_I2C_WRITE;
    138  1.1  riastrad 		}
    139  1.1  riastrad 		while (remaining) {
    140  1.1  riastrad 			if (remaining > max_bytes)
    141  1.1  riastrad 				current_count = max_bytes;
    142  1.1  riastrad 			else
    143  1.1  riastrad 				current_count = remaining;
    144  1.1  riastrad 			ret = radeon_process_i2c_ch(i2c,
    145  1.1  riastrad 						    p->addr, flags,
    146  1.1  riastrad 						    &p->buf[buffer_offset], current_count);
    147  1.1  riastrad 			if (ret)
    148  1.1  riastrad 				return ret;
    149  1.1  riastrad 			remaining -= current_count;
    150  1.1  riastrad 			buffer_offset += current_count;
    151  1.1  riastrad 		}
    152  1.1  riastrad 	}
    153  1.1  riastrad 
    154  1.1  riastrad 	return num;
    155  1.1  riastrad }
    156  1.1  riastrad 
    157  1.1  riastrad u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap)
    158  1.1  riastrad {
    159  1.1  riastrad 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
    160  1.1  riastrad }
    161  1.1  riastrad 
    162