资讯详情

龙芯上的温度传感器

驱动模型与系统下读取模式略有不同, 2.6.32内核(以下为2h为例分析) linux下驱动文件:

arch/mips/loongson2/loongson2hsoc/temperature.c ////具体实现温度采集 
#define CPU0_SENSOR_BASE 0x900000001fef0000 static void temp_handler(void) { 
            unsigned char temp;     unsigned int tmp;      temp = *((volatile unsigned char *)temp_device.resource[0].start);     //if (temp >= WARNING_TEMP)         printk(KERN_INFO "CPU temperature! %d C\n", temp);      //if (temp >= POWEROFF_TEMP)     // goto poweroff;       cpu_temp.cpu_num = 1;     cpu_temp.temp    = *((volatile unsigned char *)temp_device.resource[0].start);     mod_timer(&timer_temp, jiffies   3 * HZ);      tmp    = *((volatile unsigned int*)temp_device.resource[0].start);     printk("reg val %u\n",tmp);      return;  poweroff:/span> printk(KERN_INFO " System will be poweroff.\n"); pm_power_off(); } 

arch/mips/loongson2/loongson2hsoc/platform.c //cpu传感器具体资源信息; linux下读取方式:

	cat /proc/cputemp 

3.10的内核(下面以2k为例分析) 驱动主要文件: arch/mips/loongson2/dts/2k1000_board.dts //dts配置文件,监视器模块

        hwmon@0x1fe07000{ 
       
            #address-cells = <1>;
            #size-cells = <1>;

            compatible = "loongson,ls-hwmon";
            reg = <0x1fe07000 0x1>;
            max-id = <0>;
            id = <0>;
        };

./drivers/platform/mips/cpu_hwmon.c //驱动主要实现文件

#ifdef CONFIG_OF
static struct of_device_id ls_hwmon_id_table[] = { 
        
    { 
        .compatible = "loongson,ls-hwmon" },
    { 
       }, 
};
#endif

static struct platform_driver ls_hwmon_driver = { 
        
    .probe  = ls_hwmon_probe,
    .remove = ls_hwmon_remove,
    .driver = { 
        
        .name = "ls-hwmon",
        .owner = THIS_MODULE,
#ifdef CONFIG_OF
        .of_match_table = of_match_ptr(ls_hwmon_id_table),
#endif
    },  
};
static int __init ls_hwmon_init_driver(void)
{ 
       
#ifdef CONFIG_CPU_LOONGSON3  //2k上未定义
    if(cpu_guestmode)
        return 0;
    if (boot_cpu_type() == CPU_LOONGSON3_COMP)
        csr_temp_enable = read_csr(LOONGSON_CPU_FEATURE_OFFSET) & LOONGSON_CPU_FEATURE_TEMP;
#endif
    loongson_hwmon_init();
    return platform_driver_register(&ls_hwmon_driver);
}
static int __init loongson_hwmon_init(void)
{ 
       
    int ret;

    printk(KERN_INFO "Loongson Hwmon Enter...\n");

    cpu_hwmon_dev = hwmon_device_register(NULL);
    if (IS_ERR(cpu_hwmon_dev)) { 
       
        ret = -ENOMEM;
        printk(KERN_ERR "hwmon_device_register fail!\n");
        goto fail_hwmon_device_register;
    }

    ret = sysfs_create_group(&cpu_hwmon_dev->kobj,
                &cpu_hwmon_attribute_group);
    if (ret) { 
       
        printk(KERN_ERR "fail to create loongson hwmon!\n");
        goto fail_sysfs_create_group_hwmon;
    }

    ret = create_sysfs_cputemp_files(&cpu_hwmon_dev->kobj);
    if (ret) { 
       
        printk(KERN_ERR "fail to create cpu temprature interface!\n");
        goto fail_create_sysfs_cputemp_files;
    }

    INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer);
    return ret;

fail_create_sysfs_cputemp_files:
    sysfs_remove_group(&cpu_hwmon_dev->kobj,
                &cpu_hwmon_attribute_group);

fail_sysfs_create_group_hwmon:
    hwmon_device_unregister(cpu_hwmon_dev);

fail_hwmon_device_register:
    return ret;
}
#define CPU_THERMAL_THRESHOLD 98000
static struct delayed_work thermal_work;

static void do_thermal_timer(struct work_struct *work)
{ 
       
    int value = loongson_cpu_temp(0);
    if ((value <= CPU_THERMAL_THRESHOLD) || (loongson_hwmon == 0)) { 
       
        schedule_delayed_work(&thermal_work, msecs_to_jiffies(5000));
    } else { 
       
        pr_emerg("!!!High temperature: %d. Power-off NOW!\n", value);
        orderly_poweroff(true);
    }
}


static int ls_hwmon_probe(struct platform_device *pdev) //主要函数
{ 
       
    struct resource *mem = NULL;
    struct device_node *np;
    struct ls_temp_id *hwmon_id;
    int ret;
    int id = 0;
    int max_id = 0;

    np = of_node_get(pdev->dev.of_node);
    if(np){ 
       
        if(of_property_read_u32(np, "max-id", &max_id))
            pr_err("not found max-id property!\n");

        if(of_property_read_u32(np, "id", &id))
            pr_err("not found id property!\n");
    }else{ 
       
        hwmon_id = pdev->dev.platform_data;
        max_id = hwmon_id->max_id;
        id = pdev->id;
    }

    mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    if (!mem){ 
       
        ret = -ENODEV;
        goto out;
    }

    if(!request_mem_region(mem->start, resource_size(mem), pdev->name)){ 
       
        ret = -EBUSY;
        goto out;
    }

    loongson_chiptemp[id] = (u64)ioremap(mem->start, resource_size(mem));

    if (id == max_id)
        schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000));
    return 0;

out:
    pr_err("%s: %s: missing mandatory property\n", __func__, pdev->name);
    return ret;
}

linux下查看cpu温度传感器方式:

	cat /sys/class/hwmon/hwmon0/temp1_input

标签: 主板传感器温度过高

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台