<commit_before>/*	$OpenBSD: gdiumiic.c,v 1.2 2010/02/19 00:21:45 miod Exp $	*/

/*
 * Copyright (c) 2010 <NAME>.
 * Copyright (c) 2006 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * I2C bus bit-banging through GPIO pins.
 *
 * Derived from /sys/dev/gpio/gpioiic.c, with SDA and SCL pin order
 * exchanged, and knowledge of bus contents added.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/gpio.h>
#include <sys/rwlock.h>

#include <dev/gpio/gpiovar.h>

#include <dev/i2c/i2cvar.h>
#include <dev/i2c/i2c_bitbang.h>

#define GPIOIIC_PIN_SDA		1
#define GPIOIIC_PIN_SCL		0
#define GPIOIIC_NPINS		2

#define GPIOIIC_SDA		0x02
#define GPIOIIC_SCL		0x01

struct gdiumiic_softc {
	struct device		sc_dev;

	void *			sc_gpio;
	struct gpio_pinmap	sc_map;
	int			__map[GPIOIIC_NPINS];

	struct i2c_controller	sc_i2c_tag;
	struct rwlock		sc_i2c_lock;

	int			sc_sda;
	int			sc_scl;
};

int		gdiumiic_match(struct device *, void *, void *);
void		gdiumiic_attach(struct device *, struct device *, void *);
int		gdiumiic_detach(struct device *, int);

int		gdiumiic_i2c_acquire_bus(void *, int);
void		gdiumiic_i2c_release_bus(void *, int);
int		gdiumiic_i2c_send_start(void *, int);
int		gdiumiic_i2c_send_stop(void *, int);
int		gdiumiic_i2c_initiate_xfer(void *, i2c_addr_t, int);
int		gdiumiic_i2c_read_byte(void *, u_int8_t *, int);
int		gdiumiic_i2c_write_byte(void *, u_int8_t, int);

void		gdiumiic_bb_set_bits(void *, u_int32_t);
void		gdiumiic_bb_set_dir(void *, u_int32_t);
u_int32_t	gdiumiic_bb_read_bits(void *);

int		gdiumiic_bustype(struct gpio_attach_args *);
void		gdiumiic_sensors_scan(struct device *,
		    struct i2cbus_attach_args *, void *);

struct cfattach gdiumiic_ca = {
	sizeof(struct gdiumiic_softc),
	gdiumiic_match,
	gdiumiic_attach,
	gdiumiic_detach
};

struct cfdriver gdiumiic_cd = {
	NULL, "gdiumiic", DV_DULL
};

static const struct i2c_bitbang_ops gdiumiic_bbops = {
	gdiumiic_bb_set_bits,
	gdiumiic_bb_set_dir,
	gdiumiic_bb_read_bits,
	{ GPIOIIC_SDA, GPIOIIC_SCL, GPIOIIC_SDA, 0 }
};

#define	GDIUMIIC_BUSTYPE_SENSORS	0
#define	GDIUMIIC_BUSTYPE_VIDCTRL	1

int
gdiumiic_bustype(struct gpio_attach_args *ga)
{
	extern int gdium_revision;

	/*
	 * Hardware pin connections depend upon the motherboard revision.
	 * XXX magic numbers (should match kernel configuration)
	 */
	switch (gdium_revision) {
	case 0:
		if (ga->ga_offset == 46 && ga->ga_mask == 0x03)
			return GDIUMIIC_BUSTYPE_SENSORS;
		break;
	default:
		if (ga->ga_offset == 6 && ga->ga_mask == 0x81)
			return GDIUMIIC_BUSTYPE_SENSORS;
		break;
	}

	if (ga->ga_offset == 41 && ga->ga_mask == 0x03)
		return GDIUMIIC_BUSTYPE_VIDCTRL;

	return -1;

}

int
gdiumiic_match(struct device *parent, void *match, void *aux)
{
	struct cfdata *cf = match;
	struct gpio_attach_args *ga = aux;

	if (ga->ga_offset == -1 || gdiumiic_bustype(ga) < 0)
		return 0;

	return (strcmp(cf->cf_driver->cd_name, "gdiumiic") == 0);
}

void
gdiumiic_attach(struct device *parent, struct device *self, void *aux)
{
	struct gdiumiic_softc *sc = (struct gdiumiic_softc *)self;
	struct gpio_attach_args *ga = aux;
	struct i2cbus_attach_args iba;
	int caps;

	/* Check that we have enough pins */
	if (gpio_npins(ga->ga_mask) != GPIOIIC_NPINS) {
		printf(": invalid pin mask\n");
		return;
	}

	/* Map pins */
	sc->sc_gpio = ga->ga_gpio;
	sc->sc_map.pm_map = sc->__map;
	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
	    &sc->sc_map)) {
		printf(": can't map pins\n");
		return;
	}<commit_msg>only match on Gdium; ok miod@<commit_after>/*	$OpenBSD: gdiumiic.c,v 1.3 2010/02/23 12:21:01 otto Exp $	*/

/*
 * Copyright (c) 2010 <NAME>.
 * Copyright (c) 2006 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * I2C bus bit-banging through GPIO pins.
 *
 * Derived from /sys/dev/gpio/gpioiic.c, with SDA and SCL pin order
 * exchanged, and knowledge of bus contents added.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/gpio.h>
#include <sys/rwlock.h>

#include <machine/autoconf.h>
#include <mips64/archtype.h>

#include <dev/gpio/gpiovar.h>

#include <dev/i2c/i2cvar.h>
#include <dev/i2c/i2c_bitbang.h>

#define GPIOIIC_PIN_SDA		1
#define GPIOIIC_PIN_SCL		0
#define GPIOIIC_NPINS		2

#define GPIOIIC_SDA		0x02
#define GPIOIIC_SCL		0x01

struct gdiumiic_softc {
	struct device		sc_dev;

	void *			sc_gpio;
	struct gpio_pinmap	sc_map;
	int			__map[GPIOIIC_NPINS];

	struct i2c_controller	sc_i2c_tag;
	struct rwlock		sc_i2c_lock;

	int			sc_sda;
	int			sc_scl;
};

int		gdiumiic_match(struct device *, void *, void *);
void		gdiumiic_attach(struct device *, struct device *, void *);
int		gdiumiic_detach(struct device *, int);

int		gdiumiic_i2c_acquire_bus(void *, int);
void		gdiumiic_i2c_release_bus(void *, int);
int		gdiumiic_i2c_send_start(void *, int);
int		gdiumiic_i2c_send_stop(void *, int);
int		gdiumiic_i2c_initiate_xfer(void *, i2c_addr_t, int);
int		gdiumiic_i2c_read_byte(void *, u_int8_t *, int);
int		gdiumiic_i2c_write_byte(void *, u_int8_t, int);

void		gdiumiic_bb_set_bits(void *, u_int32_t);
void		gdiumiic_bb_set_dir(void *, u_int32_t);
u_int32_t	gdiumiic_bb_read_bits(void *);

int		gdiumiic_bustype(struct gpio_attach_args *);
void		gdiumiic_sensors_scan(struct device *,
		    struct i2cbus_attach_args *, void *);

struct cfattach gdiumiic_ca = {
	sizeof(struct gdiumiic_softc),
	gdiumiic_match,
	gdiumiic_attach,
	gdiumiic_detach
};

struct cfdriver gdiumiic_cd = {
	NULL, "gdiumiic", DV_DULL
};

static const struct i2c_bitbang_ops gdiumiic_bbops = {
	gdiumiic_bb_set_bits,
	gdiumiic_bb_set_dir,
	gdiumiic_bb_read_bits,
	{ GPIOIIC_SDA, GPIOIIC_SCL, GPIOIIC_SDA, 0 }
};

#define	GDIUMIIC_BUSTYPE_SENSORS	0
#define	GDIUMIIC_BUSTYPE_VIDCTRL	1

int
gdiumiic_bustype(struct gpio_attach_args *ga)
{
	extern int gdium_revision;

	/*
	 * Hardware pin connections depend upon the motherboard revision.
	 * XXX magic numbers (should match kernel configuration)
	 */
	switch (gdium_revision) {
	case 0:
		if (ga->ga_offset == 46 && ga->ga_mask == 0x03)
			return GDIUMIIC_BUSTYPE_SENSORS;
		break;
	default:
		if (ga->ga_offset == 6 && ga->ga_mask == 0x81)
			return GDIUMIIC_BUSTYPE_SENSORS;
		break;
	}

	if (ga->ga_offset == 41 && ga->ga_mask == 0x03)
		return GDIUMIIC_BUSTYPE_VIDCTRL;

	return -1;

}

int
gdiumiic_match(struct device *parent, void *match, void *aux)
{
	struct cfdata *cf = match;
	struct gpio_attach_args *ga = aux;

	if (ga->ga_offset == -1 || gdiumiic_bustype(ga) < 0)
		return 0;

	return (sys_platform->system_type == LOONGSON_GDIUM &&
	    strcmp(cf->cf_driver->cd_name, "gdiumiic") == 0);
}

void
gdiumiic_attach(struct device *parent, struct device *self, void *aux)
{
	struct gdiumiic_softc *sc = (struct gdiumiic_softc *)self;
	struct gpio_attach_args *ga = aux;
	struct i2cbus_attach_args iba;
	int caps;

	/* Check that we have enough pins */
	if (gpio_npins(ga->ga_mask) != GPIOIIC_NPINS) {
		printf(": invalid pin mask\n");
		return;
	}

	/* Map pins */
	sc->sc_gpio = ga->ga_gpio;
	sc->sc_map.pm_map = sc->__map;
	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
	    &sc->sc_map)) {
		printf(": can't map pins\n");
		return;
	}
{'fraction_non_alphanumeric': 0.10773545220845993, 'fraction_numerical': 0.019630754849263847, 'mean_word_length': 4.568640208197788, 'pattern_counts': {'":': 4, '<': 31, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4597926', 'n_tokens_mistral': 3709, 'n_tokens_neox': 3491, 'n_words': 859}