Home | History | Annotate | Line # | Download | only in scmdctl
      1 /*	$NetBSD: i2cspi.c,v 1.1 2021/12/07 17:39:55 brad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2021 Brad Spencer <brad (at) anduin.eldar.org>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 #ifdef __RCSID
     20 __RCSID("$NetBSD: i2cspi.c,v 1.1 2021/12/07 17:39:55 brad Exp $");
     21 #endif
     22 
     23 /* Functions that know how to talk to scmd(4) */
     24 
     25 #include <inttypes.h>
     26 #include <stdbool.h>
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <unistd.h>
     30 #include <err.h>
     31 #include <fcntl.h>
     32 #include <string.h>
     33 #include <limits.h>
     34 #include <errno.h>
     35 
     36 #include <dev/ic/scmdreg.h>
     37 
     38 #include "scmdctl.h"
     39 #include "responses.h"
     40 
     41 #define EXTERN
     42 #include "i2cspi.h"
     43 
     44 int	i2cspi_clear(int, bool);
     45 int     i2cspi_read_register(int, bool, int, uint8_t, uint8_t, uint8_t *);
     46 int	i2cspi_write_register(int, bool, int, uint8_t, uint8_t);
     47 
     48 
     49 /* The scmd(4) presents the register space as a linear chunk that can
     50  * be seek to read and written to without any real messing around
     51  */
     52 static int
     53 i2cspi_phy_read_register(int fd, bool debug, uint8_t reg, int rlen, uint8_t *buf)
     54 {
     55 	int err;
     56 
     57 	if (rlen <= 0)
     58 		return EINVAL;
     59 
     60 	err = lseek(fd, reg, SEEK_SET);
     61 	if (err != -1) {
     62 		err = read(fd, buf, rlen);
     63 		if (err == -1)
     64 			err = errno;
     65 		else
     66 			err = 0;
     67 	} else {
     68 		err = errno;
     69 	}
     70 
     71 	if (debug)
     72 		fprintf(stderr,"i2cspi_phy_read_register: reg: 0x%02X: rlen: %d: return err: %d\n",reg, rlen, err);
     73 
     74 	return err;
     75 }
     76 
     77 static int
     78 i2cspi_phy_write_register(int fd, bool debug, uint8_t reg, uint8_t buf)
     79 {
     80 	int err;
     81 
     82 	err = lseek(fd, reg, SEEK_SET);
     83 	if (err != -1) {
     84 		err = write(fd, &buf, 1);
     85 		if (err == -1)
     86 			err = errno;
     87 		else
     88 			err = 0;
     89 	} else {
     90 		err = errno;
     91 	}
     92 
     93 	if (debug)
     94 		fprintf(stderr,"i2cspi_phy_write_register: reg: 0x%02X: return err: %d\n",reg, err);
     95 
     96 	return err;
     97 }
     98 
     99 int
    100 i2cspi_clear(int fd, bool debug)
    101 {
    102 	return 0;
    103 }
    104 
    105 int
    106 i2cspi_read_register(int fd, bool debug, int a_module, uint8_t reg, uint8_t reg_end, uint8_t *r)
    107 {
    108 	if (reg > SCMD_LAST_REG ||
    109 	    reg_end > SCMD_LAST_REG)
    110 		return EINVAL;
    111 
    112 	if (reg_end < reg)
    113 		return EINVAL;
    114 
    115 	if (debug)
    116 		fprintf(stderr,"i2cspi_read_register: reg: %d ; reg_rend: %d ; rlen: %d\n",reg, reg_end, reg_end - reg + 1);
    117 
    118 	return i2cspi_phy_read_register(fd, debug, reg + (SCMD_REG_SIZE * a_module), reg_end - reg + 1, r);
    119 }
    120 
    121 int
    122 i2cspi_write_register(int fd, bool debug, int a_module, uint8_t reg, uint8_t reg_v)
    123 {
    124 	int err;
    125 
    126 	if (reg > SCMD_LAST_REG)
    127 		return EINVAL;
    128 
    129 	err = i2cspi_phy_write_register(fd, debug, reg + (SCMD_REG_SIZE * a_module), reg_v);
    130 
    131 	return err;
    132 }
    133