📄 lm70_mediaengine.c.txt
字号:
/*
* LM70_mediaengine v1.0 11/05/01
* www.embeddedlinuxinterfacing.com
*
* The original location of this code is
* http://www.embeddedlinuxinterfacing.com/chapters/10/
*
* Copyright (C) 2001 by Craig Hollabaugh
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* LM70_mediaengine.c is based on procfs_example.c by Erik Mouw.
* For more information, please see The Linux Kernel Procfs Guide, Erik Mouw
* http://kernelnewbies.org/documents/kdoc/procfs-guide/lkprocfsguide.html
*/
/* LM70_mediaengine
* This device driver demonstrates communication with a LM70 temperature
* sensor using the SA1110 SPI controller. The driver uses polled I/O
* by checking the SSSR's busy flag instead of using interrupts.
* The driver creates a /proc directory entry called
* /proc/trailblazer/temperature. Scripts can read the current temperature
* from this file.
*/
/*
arm-linux-gcc -O2 -D__KERNEL__ -DMODULE -I/usr/src/arm-linux/include -c LM70_mediaengine.c -o /tftpboot/arm-rootfs/tmp/LM70_mediaengine.o
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <asm/io.h>
#define MODULE_VERSION "1.0"
#define MODULE_NAME "LM70_mediaengine"
static struct proc_dir_entry *tb_dir,
*temperature_file;
/* see 11.12 Intel StrongARM SA-1110 Microprocessor Developer's Manual */
#define SSP 0x80070000
#define SSCR0_OFFSET 0x60
#define SSCR1_OFFSET 0x64
#define SSDR_OFFSET 0x6C
#define SSSR_OFFSET 0x74
#define SSPLEN 0x78
/* SSP bits */
#define SPP_RNE 0x04
#define SPP_BSY 0x08
static void *ssp_base;
/*
* function getTemperature
* This function performs SPI communications with the LM70
* using programmed I/O (polled mode). Prior to SPI communication,
* the SA-1110 receive FIFO buffer will be cleared. The getTemperature
* function will start SPI communication then continuously monitor the
* SSP busy flag in the SSP Status Register (SSSR). Upon SPI
* communication completion, the SSP Data Register (SSDR) will contain
* the 16-bit temperature reading.
* returns
* All 16 bits are returned, 11 temperature value bits, 3 1 bits
* and 2 undefined bits.
*
* This routine doesn't perform communication error checking or
* reporting.
*
*/
int getTemperature(void)
{
unsigned char i;
int temperature, status;
/* need to flush the receive FIFO before we start */
for (i = 0; i < 16; i++)
{
status = readw(ssp_base + SSSR_OFFSET); /* read the status register */
if ((status & SPP_RNE) == 0) /* is the receive FIFO empty? */
break;
temperature = readw(ssp_base + SSDR_OFFSET);
/* read the receive FIFO to clear out old stuff */
}
/* start SPI communication, need to feed the transmit FIFO a dummy value */
writel(0x00, ssp_base + SSDR_OFFSET);
/* now wait until the BSY flag is not set and read the receive FIFO */
for (i = 0; i < 20; i++)
{
status = readw(ssp_base + SSSR_OFFSET); /* read the status register */
if ((status & SPP_BSY) == 0) /* are we still doing an SPI frame? */
break;
udelay(1000); /* wait a little */
}
temperature = readw(ssp_base + SSDR_OFFSET); /* read the receive FIFO */
/* for debugging uncomment the next line */
/* printk("temp: i is %d, temperature is 0x%04X\n",i, temperature); */
/*
* Now we to scale the temperature value. Simply shifting the
* bits right doesn't perform the correct operation if the
* temperature is below zero. Shifting introduces 0's into the
* MSB thus losing the negative sign bit. An integer divide by 32
* preserves the sign bit and eliminates the last unused 5 bits
*
* At this point, after the divide by 32. temperature contains
* the count of LSB bits where 1 lSB equals 0.25C. An additional
* divide by 4 scales temperature in 1 degree increments.
*/
temperature /= 128; /* 32 * 4 = 128 */
return temperature;
}
/*
* function proc_read_temperature
* This function is called when the user performs a read operation
* on /proc/trailblazer/temperature. This function fills the page
* buffer with a temperature in degrees C.
*
*/
static int proc_read_temperature(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
int len, temp;
temp = getTemperature();
len = sprintf(page, "%+d\n", temp); /* temp * 9 / 5 + 32 here for F */
return len;
}
/*
* function init_LM70_mediaengine
* This initialization function creates the /proc/trailblazer
* directory and a temperature entry in it then initializes the
* LM70 temperature sensor
*
*/
static int __init init_LM70_mediaengine(void)
{
unsigned int r;
int rv = 0;
/* Create the trailblazer /proc entry */
tb_dir = proc_mkdir("trailblazer", NULL);
if(tb_dir == NULL) {
rv = -ENOMEM;
goto out;
}
tb_dir->owner = THIS_MODULE;
/* Create temperature and make it readable by all - 0444 */
temperature_file = create_proc_entry("temperature", 0444, tb_dir);
if(temperature_file == NULL) {
rv = -ENOMEM;
goto no_temperature;
}
temperature_file->data = NULL;
temperature_file->read_proc = &proc_read_temperature;
temperature_file->write_proc = NULL;
temperature_file->owner = THIS_MODULE;
ssp_base = ioremap_nocache(SSP,SSPLEN);
printk("ssp_base = 0x%08X\n",ssp_base);
/*
* SSCR1 binary value 0000 0000 0000 0000 = 0x0000
* External Clock Select (ECS), 0 uses internal clock
* Serial Clock Phase (SPH), 0 adds 1 clock cycle delay
* Serial Clock Polarity (SPO), 0 holds SC low during idle
* Loopback Mode (LBM), 0 disables loopback mode
* Transmit FIFO Interrupt Enable (TIE), 0 masks interrupt
* Receive FIFO Interrupt Enable (RIE), 0 masks interrupt
*/
writel(0x0000, ssp_base + SSCR1_OFFSET);
/*
* SSCR0 binary value 1111 1111 1000 1111 = 0xFF8F
* Data Size Select (DSS), 1111 selects 16 bit length
* Frame Format (FRF), 00 selects Motorola SPI
* Synchronous Serial Port Enable (SSE), 1 for enable
* Serial Clock Rate (SCR), 11111111 selects slowest clock
*/
writel(0xFF8F, ssp_base + SSCR0_OFFSET);
/* read the registers and printout results */
r = readl(ssp_base + SSCR1_OFFSET);
printk("SSCR1 = 0x%04X\n",r);
r = readl(ssp_base + SSCR0_OFFSET);
printk("SSCR0 = 0x%04X\n",r);
/* everything initialized */
printk(KERN_INFO "%s %s initialized\n", MODULE_NAME, MODULE_VERSION);
return 0;
no_temperature:
remove_proc_entry("trailblazer", NULL);
out:
return rv;
}
/*
* function cleanup_LM70_mediaengine
* This clean function powers down the sensor and removes
* the /proc/trailblazer directory and the temperature entry.
*/
static void __exit cleanup_LM70_mediaengine(void)
{
iounmap(ssp_base);
remove_proc_entry("temperature", tb_dir);
remove_proc_entry("trailblazer", NULL);
printk(KERN_INFO "%s %s removed\n", MODULE_NAME, MODULE_VERSION);
}
module_init(init_LM70_mediaengine);
module_exit(cleanup_LM70_mediaengine);
MODULE_AUTHOR("Craig Hollabaugh");
MODULE_DESCRIPTION("Trailblazer LM70_mediaengine");
EXPORT_NO_SYMBOLS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -