#include #include #include #include #include #include #include #include #include "ls7366r_registers.h" #include "ls7366r.h" /* spi stuff */ static int dt_ls7366r_probe(struct spi_device *client); static void dt_ls7366r_remove(struct spi_device *client); /* LS7366R functions */ static int writeCmd(struct spi_device *client, u8 c) { u8 buf[1]; buf[0] = c; return spi_write(client, buf, 1); } static int writeCmdData(struct spi_device *client, u8 c, u8 d) { u8 buf[2]; buf[0] = c; buf[1] = d; return spi_write(client, buf, 2); } static int read32(struct spi_device *client, u8 c, u32 *r) { u8 tx_buf[5]; tx_buf[0] = c; u8 rx_buf[5]; struct spi_transfer t = { .tx_buf = tx_buf, .rx_buf = rx_buf, .len = 5, }; int ret = spi_sync_transfer(client, &t, 1); if (ret < 0) { printk("counter - Error! spi_sync_transfer failed in read32\n"); return ret; } *r = ((u32)rx_buf[1] << 24) | ((u32)rx_buf[2] << 16) | ((u32)rx_buf[3] << 8) | ((u32)rx_buf[4]); return 0; } int read_otr(struct spi_device *client, u32 *r) { return read32(client, CMD_RD | REG_OTR, r); } static int read8(struct spi_device *client, u8 c, u8 *r) { u8 tx_buf[2]; tx_buf[0] = c; u8 rx_buf[2]; struct spi_transfer t = { .tx_buf = tx_buf, .rx_buf = rx_buf, .len = 2, }; int ret = spi_sync_transfer(client, &t, 1); if (ret < 0) { printk("counter - Error! spi_sync_transfer failed in read8\n"); return ret; } *r = rx_buf[1]; return 0; } static struct of_device_id ls7366r_driver_ids[] = { { .compatible = "hottis,ls7366r", }, {} }; MODULE_DEVICE_TABLE(of, ls7366r_driver_ids); static struct spi_device_id ls7366r[] = { { "ls7366r", 0 }, {}, }; MODULE_DEVICE_TABLE(spi, ls7366r); struct spi_driver ls7366r_driver = { .probe = dt_ls7366r_probe, .remove = dt_ls7366r_remove, .id_table = ls7366r, .driver = { .name = "ls7366r_device_driver", .of_match_table = ls7366r_driver_ids, }, }; struct spi_device *ls7366r_spi_client; static int dt_ls7366r_probe(struct spi_device *client) { printk("counter - ls7366r probing\n"); int ret = spi_setup(client); if (ret < 0) { printk("counter - Error! Failed to set up the SPI bus\n"); return ret; } ret = writeCmd(client, CMD_CLR | REG_STR); if (ret < 0) { printk("counter - Error! writeCmd 1 failed in probe\n"); return ret; } ret = writeCmd(client, CMD_CLR | REG_CNTR); if (ret < 0) { printk("counter - Error! writeCmd 2 failed in probe\n"); return ret; } ret = writeCmdData(client, CMD_WR | REG_MDR0, MDR0_ILO); if (ret < 0) { printk("counter - Error! writeCmdData failed in probe\n"); return ret; } u8 x; ret = read8(client, CMD_RD | REG_MDR0, &x); if (ret < 0) { printk("counter - Error! read8 failed in probe\n"); return ret; } if (x != MDR0_ILO) { printk("counter - Error! failed to initialize counter\n"); return -1; } ls7366r_spi_client = client; return 0; } static void dt_ls7366r_remove(struct spi_device *client) { printk("counter - ls7366r removing\n"); }