From patchwork Mon Jul 25 23:51:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Brace X-Patchwork-Id: 12928527 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7BAB3C433EF for ; Mon, 25 Jul 2022 23:53:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E333210F60B; Mon, 25 Jul 2022 23:53:40 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1F79C10F62A for ; Mon, 25 Jul 2022 23:53:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1658793195; bh=FTS0WBHVw5mYjOegRVw3nfhyqrC1r3khqrbIVBtc0Ps=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=Hi68s3XBrZ0X7GkeZR2HdcFjOAKc4LzSrACFC9Y6NA0VPqleAQu4/lU/4W/tNw8bR /yEvPkDSSyHTPvad91+gQL3SOy7nB4gPqOG2rZxf2B7NcGmvVsQ0y/8JQCjqzjx4sm lfQcDohVNXWHXas1OV12fo/86D2RovCc1+iYBjkU= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from localhost.localdomain ([108.233.15.105]) by mail.gmx.net (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MeU4s-1nfxeg1EHs-00aWaC; Tue, 26 Jul 2022 01:53:15 +0200 From: Kevin Brace To: dri-devel@lists.freedesktop.org Subject: [PATCH v3 15/32] drm/via: Add via_i2c.c Date: Mon, 25 Jul 2022 16:51:10 -0700 Message-Id: <20220725235127.20433-16-kevinbrace@gmx.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220725235127.20433-1-kevinbrace@gmx.com> References: <20220725235127.20433-1-kevinbrace@gmx.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:DaW+t5/InCMfE+TdOqd3s+sgQpkbw+zH5/he2pNJrViyxqOsQqk LUQFTn0HVf8OFOx58uwL5cdB6ZDGGhGakuf6YvHxp/0sW7bgDkt+9Za9ApHCg80bZqb04tR hhNOJ9PnVyA7lyyUQxphQ7Z0J1hfeM75A2dzvYphH1sYFCqr9WxmkLFstL1klP/kdRnN0Pt 4k7hL1XMXLDzWzTcQ1vog== X-UI-Out-Filterresults: notjunk:1;V03:K0:xV76oV6MF5E=:7AYe3NC09lkaW9V7D64UwF B3xK7hHklJOZpKOrgeJz+WdfGnJQweZkaSeB9988AzK5Cgo2YrGvIMalQwYKArBpNU9aZps4G RCE+C09wHe0C+61hWXqXJJUdJsarf0N56gfsS4EekridWz24Fe8QtCZHFMJ54xWv0aVRfNE7e D2ueKSMDBPrCLnmHkSLE8B70iLO1H6ZS6F7w0UHBGPUi9/EnduufbOTjk1jQ6/iOrKVvILV9K 2rAoBznjFR3U/gpELDXJ5TDkXxE5QVp5zDJj0DGsYUI8gs8NeKViR+emUqDaSVwZV3ZnCS3Ns PKgf/Vw0PeeN9aNW5Hr8I+lSQFAX1unwXYLUv2Onv8vtmh5OocI8e7gpzrIqGI4VFxJ0LToOx ve0e5FuY8b+jCL6gEPPwIBE4gFK1wt3waFQeHnwFMs/Ll/RzCrL/Yqqd8lskPjx3z3lruw0DS k5V+vNmUDx49ZKZLHclHd/eynB9ziJPIOxtpo6Rh+zMjOYRqnXcMbY7fop6kmOvu9z8Rxb12X yyNUTiQcjjsqVT7/oLiR3OWKCIKcumCddTMZaCj9m5cK8MBtX9H2DZapFhL63Gv7u88yFO+PJ yS0efIlZUg43VRCET2JGofF4chlYWOw0MgO4gjZt4bhtLgXrxIXPX2xh43B3Sfp0vA72g9hTN 8xlOqKn4MB08/Sb3GgAkP0xyNt8rvwPc0l0n5tVU45Br8QplopJDVIleAN1svbtOiRhw+jefM vGR1ycpkVb6q7fNSZW2TICGw6JQ93Jn9tBzDqSlI7ReLa0Rlx5KerP5BrQHhaJrXbeawvO2VK 9+NpUvCBwlTiiPLAHw3s6jNPb8tdvqgYvEn8bO1myW8tSKARzkQqnSiVbnx6x9TEBiDszeMh4 sidfb127DMO5O1oc5w7SntqZDaYCc1JlAWrHU2CUAfP+jHCNP9tC52hImKeKfI5XjFrViwlL3 E0TVP0a4e8wGnaoZVlFVgMk6xmwGb9fIrX5g72ODSzCjYHKiN4wjykJPBUCcU0Gg/ENvz3K7L 1CObzz5iL2TNJPWcY+2OZUhQFy9bH/EQwKMHXjypAg4favSdDP7y7EAfHg7OINnolRAPh4ssX z5pd330rMz8ThZoApIgb1gXILMF7E7Yhz1C1JH4PWniTGzyPzNAI46nmg== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Brace Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Kevin Brace Note that the code here is GPL based. Signed-off-by: Kevin Brace --- drivers/gpu/drm/via/via_i2c.c | 209 ++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 drivers/gpu/drm/via/via_i2c.c -- 2.35.1 diff --git a/drivers/gpu/drm/via/via_i2c.c b/drivers/gpu/drm/via/via_i2c.c new file mode 100644 index 000000000000..f2e8b118754e --- /dev/null +++ b/drivers/gpu/drm/via/via_i2c.c @@ -0,0 +1,209 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright 2012 James Simmons. All Rights Reserved. + * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved. + * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved. + * + * Author(s): + * James Simmons + */ + +#include + +#include "via_drv.h" + +enum viafb_i2c_adap; + +#include + +#define SERIAL 0 +#define GPIO 1 + +static struct via_i2c_stuff via_i2c_par[5]; + +static void via_i2c_setsda(void *data, int state) +{ + struct via_i2c_stuff *i2c = data; + struct drm_device *dev = i2c_get_adapdata(&i2c->adapter); + struct via_drm_priv *dev_priv = to_via_drm_priv(dev); + u8 value, mask; + + if (i2c->is_active == GPIO) { + mask = state ? BIT(6) : BIT(6) | BIT(4); + value = state ? 0x00 : BIT(6); + } else { + value = state ? BIT(4) | BIT(0) : BIT(0); + mask = BIT(4) | BIT(0); + } + + svga_wseq_mask(VGABASE, i2c->i2c_port, value, mask); +} + +static void via_i2c_setscl(void *data, int state) +{ + struct via_i2c_stuff *i2c = data; + struct drm_device *dev = i2c_get_adapdata(&i2c->adapter); + struct via_drm_priv *dev_priv = to_via_drm_priv(dev); + u8 value, mask; + + if (i2c->is_active == GPIO) { + mask = state ? BIT(7) : BIT(7) | BIT(5); + value = state ? 0x00 : BIT(7); + } else { + value = state ? BIT(5) | BIT(0) : BIT(0); + mask = BIT(5) | BIT(0); + } + + svga_wseq_mask(VGABASE, i2c->i2c_port, value, mask); +} + +static int via_i2c_getsda(void *data) +{ + struct via_i2c_stuff *i2c = data; + struct drm_device *dev = i2c_get_adapdata(&i2c->adapter); + struct via_drm_priv *dev_priv = to_via_drm_priv(dev); + + return vga_rseq(VGABASE, i2c->i2c_port) & BIT(2); +} + +static int via_i2c_getscl(void *data) +{ + struct via_i2c_stuff *i2c = data; + struct drm_device *dev = i2c_get_adapdata(&i2c->adapter); + struct via_drm_priv *dev_priv = to_via_drm_priv(dev); + + return vga_rseq(VGABASE, i2c->i2c_port) & BIT(3); +} + +struct i2c_adapter *via_find_ddc_bus(int port) +{ + struct i2c_adapter *adapter = NULL; + int i; + + for (i = 0; i < ARRAY_SIZE(via_i2c_par); i++) { + struct via_i2c_stuff *i2c = &via_i2c_par[i]; + + if (i2c->i2c_port == port) { + adapter = &i2c->adapter; + break; + } + } + return adapter; +} + +static int create_i2c_bus(struct drm_device *dev, + struct via_i2c_stuff *i2c_par) +{ + struct i2c_adapter *adapter = &i2c_par->adapter; + struct i2c_algo_bit_data *algo = &i2c_par->algo; + + algo->setsda = via_i2c_setsda; + algo->setscl = via_i2c_setscl; + algo->getsda = via_i2c_getsda; + algo->getscl = via_i2c_getscl; + algo->udelay = 15; + algo->timeout = usecs_to_jiffies(2200); /* from VESA */ + algo->data = i2c_par; + + sprintf(adapter->name, "via i2c bit bus 0x%02x", i2c_par->i2c_port); + adapter->owner = THIS_MODULE; + adapter->class = I2C_CLASS_DDC; + adapter->algo_data = algo; + i2c_set_adapdata(adapter, dev); + + /* Raise SCL and SDA */ + via_i2c_setsda(i2c_par, 1); + via_i2c_setscl(i2c_par, 1); + udelay(20); + + return i2c_bit_add_bus(adapter); +} + +void via_i2c_readbytes(struct i2c_adapter *adapter, + u8 slave_addr, char offset, + u8 *buffer, unsigned int size) +{ + u8 out_buf[2]; + u8 in_buf[2]; + struct i2c_msg msgs[] = { + { + .addr = slave_addr, + .flags = 0, + .len = 1, + .buf = out_buf, + }, + { + .addr = slave_addr, + .flags = I2C_M_RD, + .len = size, + .buf = in_buf, + } + }; + + out_buf[0] = offset; + out_buf[1] = 0; + + if (i2c_transfer(adapter, msgs, 2) != 2) + printk(KERN_WARNING "%s failed\n", __func__); + else + *buffer = in_buf[0]; +} + +void via_i2c_writebytes(struct i2c_adapter *adapter, + u8 slave_addr, char offset, + u8 *data, unsigned int size) +{ + struct i2c_msg msg = { 0 }; + u8 *out_buf; + + out_buf = kzalloc(size + 1, GFP_KERNEL); + out_buf[0] = offset; + memcpy(&out_buf[1], data, size); + msg.addr = slave_addr; + msg.flags = 0; + msg.len = size + 1; + msg.buf = out_buf; + + if (i2c_transfer(adapter, &msg, 1) != 1) + printk(KERN_WARNING "%s failed\n", __func__); + + kfree(out_buf); +} + +void via_i2c_reg_init(struct via_drm_priv *dev_priv) +{ + svga_wseq_mask(VGABASE, 0x31, 0x30, 0x30); + svga_wseq_mask(VGABASE, 0x26, 0x30, 0x30); + vga_wseq(VGABASE, 0x2C, 0xc2); + vga_wseq(VGABASE, 0x3D, 0xc0); + svga_wseq_mask(VGABASE, 0x2C, 0x30, 0x30); + svga_wseq_mask(VGABASE, 0x3D, 0x30, 0x30); +} + +int via_i2c_init(struct drm_device *dev) +{ + int types[] = { SERIAL, SERIAL, GPIO, GPIO, GPIO }; + int ports[] = { 0x26, 0x31, 0x25, 0x2C, 0x3D }; + int count = ARRAY_SIZE(via_i2c_par), ret, i; + struct via_i2c_stuff *i2c = via_i2c_par; + + for (i = 0; i < count; i++) { + i2c->is_active = types[i]; + i2c->i2c_port = ports[i]; + + ret = create_i2c_bus(dev, i2c); + if (ret < 0) + DRM_ERROR("cannot create i2c bus %x:%d\n", + ports[i], ret); + i2c++; + } + return 0; +} + +void via_i2c_exit(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(via_i2c_par); i++) + i2c_del_adapter(&via_i2c_par->adapter); +}